VB.NET Windows Forms Button Control

In this article you will learn how to use Windows Forms Button Control in VB.NET
  • 4411

 The Windows Forms Button control allows the user to click it to perform an action. When the button is clicked, it looks as if it is being pushed in and released. Whenever the user clicks a button, the Click event handler is invoked. You place code in the Click event handler to perform any action you choose.

The text displayed on the button is contained in the Text property. If your text exceeds the width of the button, it will wrap to the next line. However, it will be clipped if the control cannot accommodate its overall height.  The Text property can contain an access key, which allows a user to "click" the control by pressing the ALT key with the access key.  The appearance of the text is controlled by the Font property and the TextAlign property.

The Button control can also display images using the Image and ImageList properties.

Designating a Windows Forms Button as the Accept Button
 
On any Windows Form you can designate a Button control to be the accept button, also known as the default button. Whenever the user presses the ENTER key, the default button is clicked regardless of which other control on the form has the focus. (The exceptions to this are when the control with focus is another button - in that case, the button with the focus will be clicked - or a multiline text box, or a custom control that traps the ENTER key.)

To designate the accept button in the designer

  1. Select the form on which the button resides.
  2. In the Properties window, set the form's AcceptButton property to the Button control's name.

To designate the accept button programmatically

  • Set the form's AcceptButton property to the appropriate Button control.

        Private Sub SetDefault(ByVal myDefaultBtn As Button)
            Me.AcceptButton = myDefaultBtn
        End Sub
Designating a Windows Forms Button as the Cancel Button
 
On any Windows Form you can designate a Button control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key, regardless of which other control on the form has the focus. Such a button is usually programmed to allow the user to quickly exit an operation without committing to any action.

To designate the cancel button in the designer

  1. Select the form on which the button resides.
  2. In the Properties window, set the form's CancelButton property to the Button control's name.

To designate the cancel button programmatically

  • Set the form's CancelButton property to the appropriate Button control.

        Private Sub SetCancelButton(ByVal myCancelBtn As Button)
            Me.CancelButton = myCancelBtn
        End Sub
Responding to Windows Forms Button Clicks
The most basic use of a Windows Forms Button control is to run some code when the button is clicked.

Clicking a Button control also generates a number of other events, such as the MouseEnter, MouseDown, and MouseUp events. If you intend to attach event handlers for these related events, be sure that their actions do not conflict. For example, if clicking the button clears information that the user has typed in a text box, pausing the mouse pointer over the button should not display a tool tip with that now-nonexistent information.

If the user attempts to double-click the Button control, each click will be processed separately; that is, the control does not support the double-click event.

To respond to a button click

  • In the button's Click event handler write the code to run. Button1_Click must be bound to the control.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MessageBox.Show("Button1 was clicked")
        End Sub

     Output :

    button.gif

Here is the Code to create a Button :

Public Class Form1

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim Button1 As New Button()
        'declaring the button, Button1
        Button1.Text = "Button"
        'setting the text to be displayed on the Button
        Button1.Location = New Point(100, 50)
        'setting the location for the Button where it should be created
        Button1.Size = New Size(60, 30)
        'setting the size of the Button
        Me.Controls.Add(Button1)
        'adding the Button that is created to the form
        'the Me keyword is used to refer to the current object, in this case the Form
    End Sub

End Class

Output :

button1.gif

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.