C# – How to use Button control?

One of the mostly used control in Windows Forms is the Button control. We use Button controls, to take ‘click’ input from the User. The best example where we use Button controls is, prompt the user for an operation to perform by providing Yes or No buttons. Depending on the user input, particular operation will continue to execute.

Button class is an associated C# class for Button control. Before we use Button control, we need to place the Button control on the Form, from where we want to use this control. We can also create Button controls dynamically on the Forms.

Properties

There are so many properties this control has. We will discuss few of them and commonly used properties, through this article.

The text entered in the Text property will be displayed on the Button control.

It has Visible property; if it sets to True, the control will be visible; otherwise, it will not visible on the Form.

We can set the style of the Button control by setting it’s FlatStyle property; through this we can set Flat, Popup, Standard and System styles. For example, below screenshot displays Standard & Popup buttons.

Enabling or disabling the button control can be done through it’s Enabled property. Disabled controls do not trigger Events and also do not receive the keyboard focus on it. From below, Disabled button is an example.

We can also place images on the Button control, by setting it’s Image property. This will gives more look and feel, instead of giving only text to the control. Image or Icon will give more meaning to the control. Another important property we need to set when we set image, is TextImageRelation property. This tells where to place the image, relative to the text on the Button control. Below screenshot shows the Button, with both text and image displayed; and with this property set to “TextBeforeImage” value.

BackgroundImage property allows to set the background. Note that, this property and Image property are different. This property sets the background picture of the control; where as, Image property displays the image on the control (foreground). You can control the way the image displays, by using BackgroundImageLayout property. It allows Tile, Center, Stretch and Zoom values. Last Button in the below image sets with background image.

We can also create the Button controls on the Form, dynamically. “Dynamic” button, from below, is an example of it. May be, we will discuss this in a separate Article.

Observe the appearance of the Buttons, when respective properties are set, from below image;

Button controls
Button controls

Events

Button controls can accept, Events. One of the mostly used event is, Click event. Once we click on the button control, the Click event will trigger and the associated event handler will execute.

Another commonly used events for this control are Enter and Leave events. These will trigger when the control becomes active and in-active state, respectively. When the control is active means, the keyboard focus is on the control and ready to accept the input from the user.

If you enjoy reading this Article, please give the feedback in below Comments section.

We will discuss more, through upcoming Articles.

(Raju)

One Comment

Add a Comment

Your email address will not be published.