|
Button Control
One of the most popular control in Visual Basic is the Button Control (previously
Command Control). They are the controls which we click and release to perform some
action. Buttons are used mostly for handling events in code, say, for sending data
entered in the form to the database and so on. The default event of the Button is
the Click event and the Button class is based on the ButtonBase class
which is based on the Control class.
Button Event
The default event of the Button is the Click event. When a Button is clicked it responds
with the Click Event. The Click event of Button looks like this in code:
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
'You place the code here to perform action when Button is clicked
End Sub
|
Working with Buttons
Well, it's time to work with Buttons. Drag a Button from the toolbox onto the
Form. The default text on the Button is Button1. Click on Button1 and select it's
properties by pressing F4 on the keyboard or by selecting
View->Properties Window from the main menu. That
displays the Properties for Button1.
Important Properties of Button1 from Properties Window:
Appearance
Appearance section of the properties window allows us to make changes to the appearance
of the Button. With the help of BackColor and Background
Image properties we can set a background color and a background image to the
button. We set the font color and font style for the text that appears on button with ForeColor and
the Font property. We change the appearance style of the
button with the FlatStyle property. We can change the text
that appears on button with the Text property and with the TextAlign property
we can set where on the button the text should appear from a predefined set of options.
Behavior
Notable Behavior properties of the Button are the Enabled and Visible properties.
The Enabled property is set to True by default which makes the button enabled and
setting it's property to False makes the button Disabled. With the Visible property
we can make the Button Visible or Invisible. The default value is set to True and
to make the button Invisible set it's property to False.
Layout
Layout properties are about the look of the Button. Note the Dock property
here. A control can be docked to one edge of its parent container or can be docked
to all edges and fill the parent container. The default value is set to none. If you
want to dock the control towards the left, right, top, bottom and center you can do
that by selecting from the button like image this property displays. With the Location property
you can change the location of the button. With the Size property you can set the
size of the button. Apart from the Dock property you can set it's size and location
by moving and stretching the Button on the form itself.
Below is the image of a Button.
Creating a Button in Code
Below is the code to create a button.
Public Class Form1 Inherits System.Windows.Forms.Form
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="Creating a 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(75,23)
'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
|
|