Home

.NET Controls: Command Buttons

 

Introduction

A Button is a Windows control used to initiate an action. From the userís standpoint, a button is useful when clicked, in which case the user positions the mouse on it and presses one of the mouseís buttons. In some programming environments, the classic button is called a command button. There are other controls that can serve as click controls and initiate the same behavior as if a button were clicked. From the programmerís standpoint, a button needs a host or container. The container could be a form, a toolbar, etc.

The most common button used in Windows applications is a rectangular control that displays a word or a short sentence that directs the user to access, dismiss, initiate an action or a suite of actions.

In a Windows Forms application, this control is implemented using the Button control from the Toolbox. Therefore, to add a button to a container, click the Button control and click on the container, which is usually a form. Once you have added the control to your application, you can set its properties using the Properties window. The Button control is implemented by the Button class. The class ancestor of the button of the .NET Framework is called ButtonBase. Therefore, to programmatically create a button, you can declare a variable of type Button. Here is an example:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits Form

    Dim btnApprove As Button

    Public Sub New()
        Me.InitializeComponent()
    End Sub

    Sub InitializeComponent()

        btnApprove = New Button
        btnApprove.Location = New Point(32, 20)
        Controls.Add(btnApprove)

        Me.StartPosition = FormStartPosition.CenterScreen
    End Sub

    Public Shared Sub main()
        Dim exo As Exercise
        exo = New Exercise
        Application.Run(exo)
    End Sub
End Class

 

 

Characteristics of a Button

For a user, the most important aspects of a button are the message it displays and the action it performs. Therefore, the default property of a button is the Text: this is the word or group of words that displays on top of the control, showing the message that would direct the user as to what the button is used for.

The most popular strings that buttons display are OK and Cancel. The OK caption is set for a form or a dialog box that informs the user of an error, an intermediary situation, or an acknowledgement of an action that was performed on the dialog box that hosts the button. The Cancel caption is useful on a button whose main parent (the form or the dialog box) would ask a question or request a follow-up action from the user. Whenever a dialog box allows the user to dismiss it without continuing the action, you should provide a button with a Cancel caption.

If you create a dialog box with more than one button, you should designate one of them as the default. A default button is one that would apply its behavior if the user presses Enter after using it. Most of the time, a default button has a thicker border. To specify the button that is the default on a dialog box, set its IsDefault property to true.

After adding a button to a form (by design or with code), you can change its caption with code by assigning the desired string to the Text property. For example, you can change the caption of a button as follows:

button1.Text = "Let it Go!"

After specifying the Text of a button, by default, it's positioned in the middle center of the button:

The position of the text of a button is controlled through the TextAlign property which is a value of the ContentAlignment enumerator. The possible values are:

TopLeft TopCenter TopRight
TopLeft TopCenter TopRight
MiddleLeft MiddleCenter MiddleRight
MiddleLeft MiddleRight
BottomLeft BottomCenter BottomRight
BottomLeft BottomCenter BottomRight

Here is an example:

Sub InitializeComponent()

        btnApprove = New Button
        btnApprove.Location = New Point(32, 20)
        btnApprove.Size = New Size(120, 48)
        btnApprove.Text = "Resume"
        btnApprove.TextAlign = ContentAlignment.BottomCenter
        Controls.Add(btnApprove)

        Me.StartPosition = FormStartPosition.CenterScreen
End Sub

Besides, or instead of, a caption, a button can display a picture on top. If you want a button to display a picture, you should first create or have a bitmap. Then, in the Properties window, use the Image field to select a bitmap or an icon. You can also programmatically assign an Image object to the Image property. Here is an example:

Sub InitializeComponent()

        btnApprove = New Button
        btnApprove.Location = New Point(32, 20)
        btnApprove.Size = New Size(120, 48)
        btnApprove.Text = "Resume"
        btnApprove.TextAlign = ContentAlignment.BottomCenter
        btnApprove.Image = Image.FromFile("C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Office\CRDFLE12.ICO")
        Controls.Add(btnApprove)

        Me.StartPosition = FormStartPosition.CenterScreen
End Sub

This would produce:

After assigning a bitmap to the button, by default, it is positioned in the middle center of the button. If the button is not equipped with a caption, this position can be ideal. If you are using both the caption and the picture, you can use the ImageAlign property, possibly in combination with the TextAlign property, to specify how the picture would be positioned with regards to the caption. The ImageAlign property also is based on the ContentAlignment enumerator. You can do this at design time in the Properties window or programmatically as follows:

Sub InitializeComponent()

        btnApprove = New Button
        btnApprove.Location = New Point(32, 20)
        btnApprove.Size = New Size(120, 48)
        btnApprove.Text = "Resume"
        btnApprove.TextAlign = ContentAlignment.BottomCenter
        btnApprove.Image = Image.FromFile("C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Office\CRDFLE12.ICO")
        btnApprove.ImageAlign = ContentAlignment.TopCenter
        Controls.Add(btnApprove)

        Me.StartPosition = FormStartPosition.CenterScreen
End Sub

This would produce:

Instead of using the Image property, you can first create an image list and add some pictures to it. Then, using the ImageList property, assign it to the button. Use the ImageIndex property to specify what picture would be displayed on the button.

A regular button displays with raised borders as originally set by the operating system. To give your button a fancy look and behavior, you can use the FlatStyle property. The FlatStyle property is based on an enumerator of the same name. It provides 4 values that are:

  • Flat: The button appears flat. When the mouse is over it, it becomes highlighted

  • Popup: The button appears flat. When the mouse is over it, the borders of the button are raised

  • Standard: The buttons appears and behave like all regular buttons you have seen

  • System: The appearance of the button depends on the operating system using it

When the user clicks a button to close a dialog box, you must always know what button was clicked. The .NET Framework provides a mechanism to help identify such a button. This is done through the DialogResult property. This property is based on the DialogResult enumerator. To set the desired value, after adding a button to a dialog box and while the button is selected, in the Properties window, click the DialogResult field and select the desired value. The possible values of this property are: None, OK, Cancel, Abort, Retry, Ignore, Yes, and No.

 

Button Events

Obviously the most important and the most intuitive event of a button occurs when clicked. This event is of type EventArgs, which indicates that it doesn't provide nor does it need any formal details about what is going on. To launch this event, you can double-click the button on the form. To create this event programmatically, first declare the Button control as Friend WithEvents. Then, implement a sub procedure whose declaration ends with Handles ButtonName.Click. Here is an example:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits Form

    Friend WithEvents btnApprove As Button

    Public Sub New()
        Me.InitializeComponent()
    End Sub
    Sub InitializeComponent()

        btnApprove = New Button
        btnApprove.Location = New Point(32, 20)
        btnApprove.Size = New Size(120, 48)
        btnApprove.Text = "Resume"
        btnApprove.TextAlign = ContentAlignment.BottomCenter
        btnApprove.Image = Image.FromFile("C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Graphics\icons\Office\CRDFLE12.ICO")
        btnApprove.ImageAlign = ContentAlignment.TopCenter
        Controls.Add(btnApprove)

        Me.StartPosition = FormStartPosition.CenterScreen
    End Sub

    Private Sub btnApproveClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApprove.Click
        MsgBox("Your employment application appear to be incomplete" & _
               vbCrLf & "Please complete it first before clicking Resume")
    End Sub

    Public Shared Sub main()
        Dim exo As Exercise
        exo = New Exercise
        Application.Run(exo)
    End Sub
End Class

This would produce:

 


Home Copyright © 2005-2012 FunctionX