Some Common VB Controls

Events and Controls
Every object has a set of events it can respond to, such as a mouse click, a mouse double-click, a
keypress, and many, many others. Each object has a default event, which is the event you use
most often for that object. For instance, the default event for a button is the click event; the
default event for a form is the load event.
In design view, if you double-click an object, the code window will open, and a new sub will be
created for the default event of that object. You can then write code that will be executed when
that event occurs for that object.
Sometimes you will want to write code for an event associated with an object, when that event is
not the default event for that object. For instance, you might want to write code that is executed
when the user moves the mouse over a button. You can create code for a different event by
doing the following:
1. Open the code window (single-click on the form name in Solution Explorer, then click
the View Code icon at the top of Solution Explorer)
2. Drop down the box at the top left of the code window and click on the name of the object
3. Drop down the box at the top right of the code window and click on the name of the
event you want to write code for
4. A sub will be created. Any code you write within that sub will be executed when the
chosen event is executed on the chosen object
Some Controls and some of their properties (Note 1: there are many other useful controls,
and many more useful properties… check them out! Note 2: properties may be set or changed
in code as well as during design)
The form:
It’s best to change the name of the form by right-clicking it in Solution Explorer, then
selecting Rename, and typing in the new name. This will change both the name of the form
and the name of its file, so they will both be the same. Make sure to leave the .vb extension.
(If you change the name of the form in Properties, the file name won’t be changed to
The text property changes the text at the top of the form. The WindowState property
allows you to choose Maximized so the form will start out maximized.
Text box:
Usually used to get input from the user. By default, text boxes only display a single line. If
the box is too small to display all the text of its text property, the rest of the text is hidden.
The Multiline property can be set to true to allow multiple lines to display. Also, the
Wordwrap property can be set to true to allow text to wordwrap if necessary to fit into the
Usually used to display output or information to the user. Label text cannot be changed by
the user. By default, labels autosize; that is, they are as large as needed to display the text
Page 1
value of their text property, and you can’t change the size of the label. This behavior can be
changed by setting the label’s autosize property to false.
Panel and Groupbox
Examples of container controls. Container controls hold other controls. The purpose of this
is to make a neatly grouped display, and/or to logically group a set of controls together for
coding purposes. The form itself is a container.
Check boxes
Allow user to provide input by checking a box. As many checkboxes as appropriate may be
checked. The Checked property of a check box is true if it is checked, and false otherwise.
Radio buttons
Allow the user to provide input by clicking a button. Within any group of radio buttons, one
and only one button is clicked at a time. A group of radio buttons means all that are in the
same container (either the form itself, or other containers). You can have multiple groups of
radio buttons by adding additional containers (usually Groupboxes or Panels). The Checked
property of a radio button is true if it is checked, and false otherwise.
Picture box
Displays an image. The image to be displayed is set in the Image property. The image is
added to the Resources of the project, so if you distribute your completed project, the image
automatically goes along with it. To make this happen, click the Picture Box image property,
and click the ellipses that appear. In the resulting Select Resource dialog box, click Project
Resourse File, then Import. Select the desired image from your file system, and click Open,
then OK. The image will be added to the project’s resources (if you want to use the same
image again, you can just select it from the list of resources in the Select Resource dialog
box). The Picture box’s SizeMode property controls how the picture will be displayed
 Normal displays as much of the image as will fit in the box’s current size
 StretchImage resizes the image so it is completely displayed in the box’s current
size, even if the image is distorted
 Autosize changes the box size to display the whole image in its original size
 CenterImage displays the image in the center of the Picturebox. If the image is
bigger than the box, the outside edges of the image are clipped to fit
 Zoom stretches or shirinks the image to fit the box, but maintains the hight/width
Useful properties many controls have in common
 Visible – false hides the control
 Enabled – false makes the control appear dim and it can’t be used
 Font – composed of many sub properties such as name, size, etc.
 BackColor – the color of the control’s background
 ForeColor – the color of the control’s foreground, which usually means the text on the
 BackgroundImage – a graphic displayed on the control’s background. If this is set, the
BackColor won’t appear, the image will show up instead.
 Location – a set of points, X and Y. X indicates the distance from the left side of the
container on which the control resides, and Y indicates the distance from the top of the
 Size – height and width
Page 2
Changing properties at run time
For all changes in properties in code, the process is the same
1. Type the name of the object, followed by a dot, then followed by the name of the
property you want to change (no spaces in all that)
2. Type the assignment operator (the equals sign)
3. On the right side of the assignment operator, type the new value for the property.
Depending on the property, this may be a string (enclosed in double quotes), a number, a
Boolean value (true or false), or some other type of value.
txtLastName.Text = “Fox”
picGrandkids.height = 36
lblMessage.Visible = False
chkCheckbox.Checked = True
txtAddress.BackColor = Color.AliceBlue
Page 3
Related flashcards
Create Flashcards