Saturday, 14 September 2013

VB.Net - Button Control

The Button control represents a standard Windows button. It is generally used to generate a Click event by providing a handler for the Click event.
Let's create a label by dragging a Button control from the Toolbox ad dropping it on the form.
VB.Net Button Control

Properties of the Button Control

The following are some of the commonly used properties of the Button control:
S.NPropertyDescription
1AutoSizeModeGets or sets the mode by which the Button automatically resizes itself.
2BackColorGets or sets the background color of the control.
3BackgroundImageGets or sets the background image displayed in the control.
4DialogResultGets or sets a value that is returned to the parent form when the button is clicked. This is used while creating dialog boxes.
5ForeColorGets or sets the foreground color of the control.
6ImageGets or sets the image that is displayed on a button control.
7LocationGets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.
8TabIndexGets or sets the tab order of the control within its container.
9TextGets or sets the text associated with this control.

Methods of the Button Control

The following are some of the commonly used methods of the Button control:
S.NMethod Name & Description
1GetPreferredSize
Retrieves the size of a rectangular area into which a control can be fitted.
2NotifyDefault
Notifies the Button whether it is the default button so that it can adjust its appearance accordingly.
3Select
Activates the control.
4ToString
Returns a String containing the name of the Component, if any. This method should not be overridden.

Events of the Button Control

The following are some of the commonly used events of the Button control:
S.NEventDescription
1ClickOccurs when the control is clicked.
2DoubleClickOccurs when the user double-clicks the Button control.
3GotFocusOccurs when the control receives focus.
4TabIndexChangedOccurs when the TabIndex property value changes.
5TextChangedOccurs when the Text property value changes.
6ValidatedOccurs when the control is finished validating.
Consult Microsoft documentation for detailed list of properties, methods and events of the Button control.

Example

In the following example, we create three buttons. In this example, let us:
  • Set captions for the buttons
  • Set some image for the button
  • Handle the click events of each buttons
Take following steps:
  1. Drag and drop a Label control on the form.
  2. Set the Text property to provide the caption "Tutorials Point".
  3. Drag and drop three buttons on the form.
  4. Using the properties window, change the Name properties of the buttons to btnMoto, btnLogo and btnExit respectively.
  5. Using the properties window, change the Text properties of the buttons to Show Moto, Show Logo and Exit respectively.
  6. Drag and Drop another button, using the properties window, set its Image property and name it btnImage.
At this stage, the form looks like:

Click the form and add following code in the code editor:
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.  
      Me.Text = "tutorialspont.com"
      btnImage.Visible = False
   End Sub
   Private Sub btnMoto_Click(sender As Object, e As EventArgs) Handles btnMoto.Click
      btnImage.Visible = False
      Label1.Text = "Simple Easy Learning"
   End Sub
   Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
      Application.Exit()
   End Sub
   Private Sub btnLogo_Click(sender As Object, e As EventArgs) Handles btnLogo.Click
      Label1.Visible = False
      btnImage.Visible = True
   End Sub
End Class
Clicking the first button, displays:
Result Window
Clicking the second button displays:
Result Window
Clicking the third button, exits the application.

No comments:

Post a Comment