---
description: Represents a button control.
---

# Button

A Button can be clicked by using the mouse, `ENTER` key, or `SPACEBAR` if the button has focus.

Set the `AcceptButton` or `CancelButton` property of a `Form` to allow users to click a button by pressing the `ENTER` or `ESC` keys even if the button does not have focus. This gives the form the behavior of a dialog box.

When you display a form using the `ShowDialog` method, you can use the `DialogResult` property of a button to specify the return value of `ShowDialog`.

{% hint style="info" %}
For a full list of properties, methods and events see the [API documentation.](http://docs.wisej.com/api)
{% endhint %}

## Features

### Icon

An icon can be shown on the button by setting the `Image`, `ImageList` or `ImageSource` property. The icon and text spacing can be adjusted using the `ImageSpacing` property.

![Button control showing icon and text placement options](https://553579532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MF1D11gPs_az3xaKusw%2Fuploads%2Fgit-blob-ab34da9d69016a8483640b2417a702b8c3608b9d%2Fimage.png?alt=media)

### Supports HTML

The Button control supports custom HTML for its value. Set the `AllowHtml` property to true. The below example contains text using the `<del>`, `<h2>`, and `<bdo>` tags.

![Button demonstrating HTML formatting capabilities](https://553579532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MF1D11gPs_az3xaKusw%2Fuploads%2Fgit-blob-ecbe5ffa46a10d5a3925e9c5f8f7e355661db20f%2Fimage.png?alt=media)

### Repeat

The Repeat functionality allows a button to raise its `Click` event repeatedly from the time it is pressed until released.

![Animated demonstration of button repeat functionality](https://553579532-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MF1D11gPs_az3xaKusw%2Fuploads%2Fgit-blob-842de6fa75ef5dd9f75f039c7b16178b6738459f%2FButtonRepeat.gif?alt=media)

## How to Center Button Text on Button With an Image

You can center the text on a button by doing:

```
button1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
button1.TextImageRelation = Wisej.Web.TextImageRelation.Overlay;
```

Alternatively, you can set TextAlign to MiddleCenter and TextImageRelation to Overlay in the designer.

However, you will notice that when setting TextImageRelation to Overlay the image becomes too large. This happens because overlay by default sets the image to "contain".

There are 4 ways to fix this and resize the image to the desired size:

1. In the designer, add an ImageList (even an empty one works) and associate with the button by doing: `button1.ImageList = imageList1;` (via code or in the designer). It will use the size set on the image list.
2. Override Button, and override OnWebRender and add `config.iconSize = new Size(20,20);`.&#x20;

```
public class MyButton: Button
	{
		protected override void OnWebRender(dynamic config)
		{
			base.OnWebRender((object)config);
			config.iconSize = new Size(20,20);
		}
	}
```

3. Attach to the ControlRendered event and add `e.Config.iconSize = new Size(20,20);`
4. Set the iconSize property of the button in the theme:\


<pre><code>"button": {
    "states": {
        "default": {
<strong>            "properties": {
</strong>                "iconSize": {
                    "width": 20,
                    "height": 20
                    }
            }
        }
    }
}
</code></pre>

## Advanced

### JavaScript Widget

| Item             | Description                                                                                                                                                     |
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Class name       | "wisej.web.Button"                                                                                                                                              |
| Theme appearance | "button", see [Themes](https://docs.wisej.com/theme-builder/theme-elements/elements).                                                                           |
| Child components | "icon" is the button's icon, if shown. "arrow" is the dropdown icon on a button with `MenuItems` set. See [JavaScript](../../concepts/javascript-object-model). |
| Source code      | [https://github.com/iceteagroup/wisej-js](https://github.com/iceteagroup/wisej-js/blob/master/wisej.web.TextBox.js)                                             |
