Lesson 1   |  Home  | Lesson 3

VB2010 Lesson 2-Working with Controls

Controls  in VB2010 are useful tools that can be placed in the form to perform various tasks. They are used to create many kinds of Windows applications. The diagram on the right is the Toolbox that contains the  controls of VB2010. They 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. Lets examine a few programs that made use of  Button, Label, TextBox , ListBox and PictureBox . You don't have to worry so much about the code because I will explain the program syntax as you progress to later lessons.

When you click on the Toolbox tab, the common controls Toolbox will appear.

2.1 Creating your first program

To create your first program,  drag the button control into the form, and change its default Text Button1 to OK in the properties window, the word OK will appear on the button in the form, as shown below:

Now click on the OK button and the code window appears. Enter the code as follows:

When you run the the program and click on the OK button, a dialog box will appear and display the "WELCOME TO VISUAL BASIC 2010" message,as shown below:

There you are, you have created your first VB2010 program.

2.2 Using the Text Box

Next I will show you how to create a simple calculator that adds two numbers using the TextBox control. 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 addition operator and the other label is to display the equal sign. The last label is to display the answer. Now change the label on the button to Calculate,then click on this button and enter the following code:

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

Dim num1, num2, product As Single

num1 = TextBox1.Text

num2 = TextBox2.Text

product = num1 + num2

Label1.Text = product

End Sub

When you run the program and enter two numbers, pressing the calculate button can let you perform addition of the two numbers.

 Bookmark and Share


Copyright  2010  Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net [Privacy Policy]




This Web Page Created with PageBreeze Free Website Builder