VB2019 VB2017 VB2015 VB2013 VB2012 VB2010 VB2008 VB6 VB Sample Codes 中文VB About Us

Lesson 2: Working with Controls

Controls  in VB2008 are useful objects that can be placed in the form to perform various tasks. They are used to create many kinds of Windows applications. The controls are available in a Toolbo, as shown in Figure 2.1 below:

Figure 2.1 Toolbox

The controls are categorized into Common Controls, Containers, Menus, Toolbars, Data, Components, Printings and Dialogs. At the moment, we will focus on the common controls. Some of the most used common controls are Button, Label, ComboBox, ListBox, PictureBox, TextBox etc. To insert a control into your form, you just need to drag the control and drop it into the form. You can reposition and resize it as you like. Let's examine a few programs that made use of Button, Label, TextBox , ListBox and PictureBox . You don't have to worry about the code yet, we will explain it later.

2.1 Using Text Box

In this program, you insert two textboxes , three labels and one button. The two textboxes are for the users to enter two numbers, one label is to display the multiplication operator and the other label is to display the equal sign. The last label is to display the answer.

Figure 2.1

The Code

Dim num1, num2, product As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
product = num1 * num2
Label3.Text = product

2.2 Using the ListBox

This program will add one item at a time as the user enter an item into the TextBox and click the Add button.

The code

Dim item As String
item = TextBox1.Text
Figure 2.2

2.3 Using the PictureBox

In this program, we insert a PictureBox and a Button into the form. Make sure to set the SizeMode property of the PictureBox to StretchImage so that the whole picture can be viewed in the picture box. Key in the code as shown below and you can load an image from a certain image file into the PictureBox.

The Code

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'To load an image into the PictureBox from an image file
PictureBox1.Image = Image.FromFile("c:\Users\Public\Pictures\Sample Pictures\Frangipani Flowers.jpg")

End Sub

Figure 2.3

Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy