LESSON 4 - How to use the VB standard controls
For this lesson we will need a new Project, call it Lesson4.vbp, which will only be used to create and try out various controls.
To refresh your memory, since the previous two lessons have been rather theoretical, you select the New tab, and Standard EXE for the type. As soon as The Form opens, you Save the new Project, give the Form a name, let's say it's Lesson4.frm and then you give the Project a name: Lesson4.vbp. Note that you do not have to specify the extensions, .frm and .vbp, because they will be assigned automatically.
The FormWe covered it too briefly in Lesson 01 so we'll go over it again.
The Form is the first object you see when you Open the application. It's the window into which all the controls will appear, where you will input data and see results.
There's not too much you can do with the form at this time. Basically, you adjust the BackColor and the StartUpPosition (where it will open on the screen when you Run it) and then you start putting controls on it.
The LabelThis is probably the first control you will master. It is used to display static text, titles and screen output from operations. The important properties to remember:
Frame & PictureBoxWhen you want to group several controls together - name and address, for example - you use a Frame. The frame backcolor can be the same as the form's and only the frame borders will be obvious, or it can be a different color and stand out.
You create the frame before the controls. When you create controls in a frame, they are tied to the frame and move with it. The frame caption is the text that appears at the top of the frame - you use it to define the group.
The PictureBox is like a Label with a picture in it instead of text. The Picture property determines the name of the file, .BMP or .GIF, that will be displayed. It can be used for a company logo, etc.
TextBox & CommandButtonThe TextBox is like a Label but, it is used to input data into the program. The data typed in is in the Text property of the control.
When the program is Run, only the controls that can be manipulated will be activated. For example, if the form contains 3 Labels, 3 TextBoxes and 3 Buttons, when it is Run, the cursor will not stop at the labels.
When the user hits the Tab key, the cursor will go to the first TextBox or Button - not necessarily the first one on the form but, the first one that was created. That is called the Tab order and you have to specify it.
On the form there is only one control at any given time that has the cursor on it - it is said to have Focus. If you type data, the control with Focus will receive it. You change the Focus with Tab or by clicking on a different control.
The CommandButton is used to initiate actions, usually by clicking on it. The Caption property determines the text to display on the face of the button. The Default property, if set to true, means that the button will be activated (same as Clicked) if the <Enter> key is hit anywhere in the form. If Cancel is set to True, the button will be activated from anywhere in the form by the <Esc> key.
Hopefully, you have now run this program several times, each time you added a new control, in fact. Admittedly, nothing much happened except to confirm that the controls were appearing in the right place on the form.
Here now is an example of the code we could write to perform simple tasks: input name and city and display the information in a label when the Continue button is clicked. The Exit button will end execution of the program and the Cancel button (or the Esc key) will clear the fields.
A few explanations: the Form_Load event occurs when the form first opens. This is where we initialize things - we want the TextBoxes and the result Label to be empty when we start off so, we set them to a blank space.
The actual processing is done after the data have been entered and we hit the Continue button. The processing logic is put in the Continue_button_clicked event.
When you hit the <Esc> key or you click on the Cancel button, you want to annul the entry you're doing and start over again. That's the same as opening the form so, we just tell the program to execute the Form_Load procedure which we already have.
The Exit button uses the pronoun Me to Unload. Me means the form currently active without having to name it.
Check boxes & Option buttonsThese two controls are used when the user must choose from a list of options. The difference between the two is that with Check boxes he can select several at one time but, with Option buttons, he must select only one among several.
The example below illustrates the use of Check boxes and Option buttons. To capture the information entered by means of these controls, you must test the property: Value. In a Check box, Value = 1 if box is checked and = 0 if not. In an Option button, Value = True if selected and = False if not.
The code, although somewhat long, is not very complicated. Most of the time, processing consists of checking the content of .Value. It is fairly standard procedure, especially with Option buttons, to ensure that at least one button has been selected and to display an error message if it has not.
Assignment 3Create the Payroll form shown below. Number of hours must be entered as well as the appropriate rate. Gross salary = rate * hours. Net salary = gross salary - deductions.
Look at "Lesson 11 - Downloads" for the solution.
If you haven't found the Visual Basic resource you're looking for,