Visual Basic 2010 Lesson 2-Working with Controls

[Lesson 1]<< [CONTENTS] >> [Lesson 3]

Controls  are objects that can be inserted into the form of VB2010 IDE for various purposes. You can write relevant code for them to perform certain tasks. The figure below shows the Toolbox that contains the controls. They are categorized into Common Controls, Containers, Menus, Toolbars, Data, Components, Printings and Dialogs. The most used common controls are Button, Label, ComboBox, ListBox, PictureBox and TextBox.

To insert a control into the form, drag the control from the tool box and drop it into the form. You can customise it according to your needs. For example, you can reposition and resize it. Let’s examine a few examples that involved Button, Label, TextBox , ListBox and PictureBox . You don’t have to worry so much about the code yet because we will explain the programme syntaxes as you progress to later lessons.

Visual Basic 2010

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

2.1 Creating your first VB2010 programme

To create your first programme, 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:

Visual Basic 2010

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:

Visual Basic 2010

There you are, you have created your first Visual Basic 2010 program.

2.2 Using the Text Box

We will show you how to create a simple calculator using the TextBox control. In this programme, insert two text boxes , three labels and one button. The two text boxes are for the user 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 will allows the program to add the two numbers.

Visual Basic 2010

[Lesson 1]<< [CONENTS] >> [Lesson 3]

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on Tumblr