Thursday 6 September 2012

VB Lesson -2


In the previous chapter, we understood how to write a simple program in Visual Basic. In this chapter, we will understand how to use elementary controls text box, label and command button. Nothing compares to hands on experience. So we learn by doing it.
Each chapter in this book contains one or more sample applications to explain how to use the topic that is discussed in the chapter.  In this chapter, we will develop a simple application that illustrates how to use text box, label and command button controls.
Text box is used to take input from user. User can key in the data into a text box with all editing facilities.

Label control is used to display a static text on the form, such as title.

Command button is used to invoke an action when user clicks on it.


Sample Application
We will develop a simple application to calculate interest with the given amount and rate of interest.  See figure 2.1 to get an idea about what we are about to develop in this chapter. The sample application has a single form with the following controls.

¨         A text box to take amount from user
¨         A text box to take rate of interest from user
¨         Two labels to display labels for text boxes
¨         A label to display calculated interest
¨         A label to display heading for interest
¨         A command button to clear the data entered by user
¨         A command button to calculate interest with the given amount and rate of interest and display the result in label.
¨         A command button to terminate the form
As we have seen in the previous chapter, an application is developed in three different stages; creating user interface, changing properties and writing code for events. So let us first create user interface. But even before that, create a new project.
Creating a new project
As soon as you start Visual Basic IDE, you will be prompted to select the type of project. Once you select the type of project, Visual Basic will create a new project of the selected type. But in some cases you are already in Visual Basic IDE and you want to start a new project.

Let us create a new project for sample application. The following is the procedure to create a new project if you are already in Visual Basic IDE. In case, you are invoking Visual Basic IDE, then Visual Basic will any way create a new project.

To create a new project:

1.      Select File-> New Project.
2.      In New Project dialog box, select the Standard EXE as the project type.
3.      Click on Ok button.
Visual Basic creates a new project with a single form.
When a new Standard Exe project is created, Visual Basic places a single form in the project with the name Form1. Initially this form will be empty (has no controls on it).

After having created a new project, now move to first step in developing an application - creating user interface.

Creating user interface
Creating user interface involves placing the required controls on the form and arranging them in the required manner. Follow the figure 2.1 while placing controls and arranging them.

You will learn more about aligning controls and changing space between controls in the next chapter. After you have placed all the controls on the form, move to next step i.e., changing properties.

Changing properties
The second step is changing required properties. As we are dealing with new controls, let us first understand what are the new properties we have to deal with.

Text property of the Text box

Text property of the text box represents the text in the text box. It can be used to either change the text in the text box or get the text entered by user in the text box.

Caption property of the Label

This property contains the text that is displayed in the label. Whether the text is completely displayed or not depends on the size of the text, size of the label and AutoSize property of the label. For more details see "Properties of Label control" section later in this chapter.

Name property
Each control and form in a Visual Basic project has a name, which is used to refer to the object.

Whenever you access a control in the code, you use the name of the control. Name of the control is set using Name property of the control.

Caption property contains the text to be displayed to the user and Name property contains the name that is to be used in the code to refer to the control.
 
The name of the control is important because it is used even in the event procedures of the control.  The name of the event procedure is formed using the name of the control and the name of the event. For example, if name of the control is cmdQuit and event is Click, then the name of event procedure will be cmdQuit_Click.


The following is the code you need to enter for command buttons in the form.

Private Sub CmdClear_Click()

  ' clear text in both the text boxes and lblresult
  txtamount.Text = ""
  txtintrate.Text = ""
  lblresult.Caption = "0"
  ' set focus to txtamount control
  txtamount.SetFocus
End Sub

Private Sub CmdCalculate_Click()
  ' calculate interst amount and place it lblresult
  Dim amount As Single
  Dim irate  As Single
  Dim iamt  As Single
 
  amount = CSng(txtamount.Text)
  irate = CSng(txtintrate.Text)
  ' calculate:  interest = amount * rate /100
  iamt = amount * irate / 100
  ' place the result in label control
  lblresult.Caption = iamt
End Sub

Private Sub cmdQuit_Click()
  Unload Me
End Sub


No comments:

Post a Comment