2.1 What are Controls
Controls in Visual Basic 2012 are objects that can be placed on the form to perform various tasks. Figure 2.1 shows the toolbox that contains the controls. 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 frequently used common controls are Button, Label, ComboBox, ListBox, PictureBox, and TextBox. To insert a control into your form in Visual Basic 2012 IDE, drag the control from the toolbox and drop it onto the form. You can reposition and resize it as you like.
2.2 Creating Your First Application
To create your first application in Visual Basic 2012, 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 program and click on the OK button, a dialog box will appear and displays the “WELCOME TO VISUAL BASIC 2012″ message, as shown in the Figure below. The argument Title: is to assign the title of the dialog box.:
2.3 Using the Text Box
Next, we will show you how to create a simple calculator that adds two numbers using the TextBox control. In this program, you insert two text boxes, three labels, and one button. The two text boxes 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 adds the two numbers.