VB - User Interface Design - Using Objects (Controls)
VB - User Interface Design - Using Objects (Controls)
Module 4
Introduction
One of the main planks of the Windows philosophy is that the
application should be user friendly as possible -it should be intuitive to
use. As a rule: "If it feels right, it must be right"
Forms and controls are the basic building blocks for creating the GUI.
Forms are objects that expose properties which define their appearance,
methods which define their behaviour, and events which define their
interactions with the user by setting the properties of the form and
writing the VB code to respond to its events, you customise the objects
to meet the requirements of your applications.
Controls are objects that are contained within from objects. Each type of
controls has its own set of properties, methods and events that make it
suitable for a particular purpose
In this module, you will learn the basic concepts of working with forms
and controls and their associated properties, methods and events.
Objectives
At the end of this module, you should be able to
Use appropriate controls to design a good user interface for your
program
Practical Exercises
Activity 4.1: -labels, textboxes and command buttons
Objective
In this activity you will explore the use of labels, textboxes and command buttons by
writing a program to perform simple arithmetic
Procedure
1. Open a New Project.
2. Add a new form onto your project and set the following properties:-
Name -frmCalculate.
Caption -A Simple Calculator.
3. Place three labels and three Text Boxes onto your form as shown in the figure below.
A Simple Calculator
First Number
Second Number
ADD NUMBERS
5. Place a Command Button as Shown in the figure and set its properties as follows:-
Name -cmdAdd
Caption -&Add Numbers
6. Double click on the command Button and write the following code:-
Note:
Using a Val lets Basic know that you want a numeric result
7. Save the form with the name "frmCalculator" in the project "Test"
8. Run the Application from the Run Menu. Type two numbers on each text box and
click on the command button to get the results.
NB: To run "frmCalculator" as the first form you have to set the Project Properties
from the Project menu In the StartUp Object Option.
Activity 4.1.1
1. Add THREE more buttons to your form and write the code for :-
-Subtracting Two Numbers
-Multiplying Two numbers
-Dividing Two Numbers
Notes:
A checkbox presents a set of choices from which a user can choose one or more
options i.e., any number of checkboxes in a set may be true at once
In the checkboxes statements:
The actions following then would be executed if the checkbox was true. You must
test the value for the click will turn the box on or off. The value of the checkbox
changes as soon as it is clicked
Objective
To learn how to use check boxes effectively
Procedure
1. In the same project (Test), add a new form and set the following properties:-
Name -frmCheckBoxes
Caption -Formatting Text in a Text Box
Bold
Italic
4. Double click the first check box (chkBold) and type the following code for the click
event:-
Private Sub chkBold_Click()
If chkBold. Value = 1 Then 'If chkBold is selected
txtDisplay.Font.Bold = True
Else 'If not Selected
txtDisplay.Font.Bold = False
End if
End Sub
5. Double click the second check box (chkitalic) and type the following code for the
click event:-
Private Sub chkitalic_Click()
If chkitalic.Value = 1 Then 'If chkitalic is selected
txtDisplay.Font.Italics = True
Else 'If not Selected
txtDisplay.Font.italics = False
End if
End Sub
6. Double click the command Button and write the following code for the click event:-
7. Save the form as frmCheckBoxes and run it to verify whether the text in the text box
can be formatted as required.
NB: To run a particular Fform when you have more than one form in a project,
select the Form by setting the project properties. Use the project properties option in
the project tools to do this.
Activity 4.2.1
Add another check box to the form for underlining the text. Give it the
appropriate name and write the required code.
Note:
Option controls are used in sets unlike in check boxes, and only one can be selected
at any one time e.g., you are either Male or Female, not both.. In this case, we shall
use the "if …Then…Else" statements.
Option is selected using the option's value property i.e., True or False
If an option is selected, it will be True and the statements following the test will be
executed e.g.,
If option2 Then……..
To make the button the default in an option group, set its value property to True at
design time. It remains selected until a user selects a different option button or the
code changes it.
To disable an option button, set its enabled property to False. When the program is
run, it will appear dimmed, meaning that it is unavailable.
Objective
To effectively use option Buttons
Procedure
1. Open the Test project from your Visual Basic.
2. From the Project Menu add a new form to your project and set the following
properties:-
Name -frmOptionButtons
Caption -Selecting Gender
3. Place a label, two option Buttons and a command button as shown in figure below:
Selecting Gender
Male
Female
5. Double click the form and write the following code for the Load event of the form:-
6. Double click the command button and type the following code for the Click event:-
Private cmdConfirm_Click()
If optMale. Value = True Then 'if Male option is selected
MsgBox "You are a Male"
Elself optFemale. Value = True Then 'if Female option selected
MsgBox "You are a Female"
Else ' If neither Male nor Female options are selected
MsgBox "C'mon You are Neither Male nor Female"
End If
End Sub
Frames are mostly used to hold sets of options or checkboxes. Thus, where there are
many sets of options to be selected from, it is appropriate to place each set of options
within a frame as each frame set is treated separately.
When a frame is made visible or invisible, all the objects within it appear or
disappear and when moved, they all move with it.
Objective
In this exercise, you will design a form that enables a student to chose one gender from
Option block1 and one course from Option Block2.
Procedure
1. Open the Test Project from your Disk.
2. From the Project menu select Add Form and add a New Form to your project, and set
the following properties:-
Name -frmFrames
Caption -Using Frames to Group Option Buttons
3. Place Two frames and place the Option Buttons in each frame as shown below:-
Male Commerce
Female Music
Law
4. Set the properties of the frames and the option buttons as shown in the table below:-
4. Double click the form and write the following code for the Form_Load(} event:-
6. Place a Command Button as shown in the figure and set the following properties:- .
Name -cmdConfirm
Caption -&Confirm Your Selection
7. Double Click on the Command Button and write the following code for the Click
event:-
Objectives
To effectively use list boxes
Procedure
1. Open the Test Project from your disk.
2. From the Project menu, add a New Form, and set the following properties:-
Name -frmCourses
Caption -Courses Form
3. Place a List Box, a Label and a Command Button on the form as shown.
Courses Form
OK
5. Double click the form and add the following code for the Form_Load event
NB: You can set the sorted property of the List Box to True in order to sort the
Items Alphabetically.
6. Double Click the Command Button and write the following code for the Click event
If 1stCourses. ListCount <> -1 Then 'if the list box is not empty
MsgBox "You Picked" & 1stCourses.Text
End if
7. Save the form as frmCourses and resave the project in order to make the form part of
the project
8. Run the application and select the Course and then click Ok
Activity 4.5.1
1. Add a Command Button which can remove item One and Three from the
List Box by clicking on it.
2. Add a Command Button to clear all the contents of the List Box
3. Add a Command Button which you can use to close/exit the form
Message Box
OK
Notes:
Menus are the simplest and clearest way of showing your users the full range of
facilities in your program, and giving access to those facilities.
For every menu, you must specify;
A caption:
A Name: This identifies it as a control so that you can add procedure to it
Position: i.e., where it fits within your menu system -on the top bar, on a first
level or lower level menu
The position of the menu item within the menu list determines the menu item
position in the system. If set against the edge, it will be in the top bar, if indented,
it will be on the sub-menu of the non-indented item above it. Use the arrows to
indent a menu item
Objectives
To create an effective main menu
Procedure
1. Open your project and add a New form. Set the following properties:
Name as 'frmMenu'
Caption as 'Main Menu Form'
2. Select Menu Editor from the Tools Menu. A dialog Box will appear as follows:-
4. For the Caption type "Forms. with Name as "mnuForms., Note that the Caption you
type appears on the List Box below, and Left Aligned.
5. To Add Sub-menus to the Forms Menu, Click On Next Button and Type the Caption
as "Calculator" with Name as "mnuFormsCalculator"
NB: To create a Sub-Menu, you have to click on the Forward Arrow (») Button
in order to indent it. The Main Menu Items are NOT indented.
You can also set the Shortcut Keys for the Menus from the Shortcut Option.
Caption: Calculator OK
Shortcut (None)
Index
HelpContentID: 0 NegotiatePosition 0 -None
Forms
…Calculator
6. Add the following menu Items to your Main Menu, as from the following Table
NB: You can Add other menus depending on the forms from that you have in
your Project Click the Ok Button after creating the menus.
NB. Forms is a main menu and has a 1st level menu. Any menu added to
submenu (e.g., cal) a 2nd level menu
7. Your Main Form will appear as follows (with the Main Menu):-
8. From the Forms Menu (in Your Form) Click on the Calculator Menu and Write the
following Code for the click Event:
Calculator
List Form
Do While Form
9. Write similar Codes for the other Forms that will be Displayed from the Main Menu.
10. Click the Exit Menu (from your form) and Type 'End' from the Click event to end the
running of the Project.
11. Save the form and Run it to test whether it can call the other forms.
The controls needed include a scroll bar, set of options and a menu. The layout is as
shown below
Procedure
1. Open your project and add a New form. Set the following properties:
Name as 'frmArithmetic'
Caption as 'Arithmetic Problem'
NB. Using number values in labels creates a problem when you statrt to
calculate with them. Since the operators - and + sign can be used with
both numbers and strings, the system can thus use the + to add 2 numbers
or join two strings into a longer one e.g.,
(iii) TxtAnswer will need code attached to its KeyPress event to check the answer
when [Enter] is pressed
When the [Enter] key is pressed, the keypress event which has a keyAscii
parameter, should check the Ascii code of the last key that was pressed by the
user against the input answer. The procedure is as follows
If enter is pressed
Get the value from the aswear box
If answear is correct
Increase the score
Else display the correct Answer
Increase the question count
Display the current score and count
NB> In the Answer and score display, we can use either + or & as a concatenator
to join the variables and the accompanying text i.e.,
(iv) The Type frame has 4 options (OptAdd, OptSub, OptTimes and OptDiv).
Code attached to their click events changes lblSumtype's Caption, and
therefore its value.
(v) HsbLevel sets the level of difficulty. It uses expressions like
(vi) LblScore displays the current score. This will be updated by checking the
routine attached to txtAnswer. To keep the score, we will need variables to
count the number of scores and of correct answers. They must be set up in the
general declaration
(vii) CmdNew generates a new problem and calculate the correct answer when
clicked. Its procedure takes the shape
3. Add and set the properties for the controls by filling in the table below
3. Click on the controls and add the necessary codes for the click Events: A few of the
codes are given below. Complete the code design.
General declarations
Dim Ans As Single 'the correct answer
Dim rtans as Integer 'right answers and
Dim qcount As Integer 'count of questions
Sub form_load()
Randomize
Qcount = 0 'count of questions
Rtans = 0 'score of correct answers
LblSuntype = "+" 'default type
HsbLevel = 0 'degree of difficulty
End Sub
Sub cmdNew_click()
-----------
End Sub
Activity
PROJECT 2 (5 marks)
In this project, you are going to design a form that can display various colour in the
picture box based on the option button selected.
Required
The Option buttons affect the picture box in such a way that when the appropriate
button is selected, the colour corresponding to the option is displayed in the picture
box
Place the options in a frame that describes the options as colours
When a name is written in the text box and the Add command clicked, the name is
displayed in the List box. Any additional names written are to be displayed
consequently
The Delete Button deletes the selected name(s) when clicked.
The Clear Button clears all the names in the list box when clicked.
Use the checkbox in such a way that when the check box is checked, a picture
(located at a predefined location in the computer) is displayed in the picture box
Use the vertical scroll bar to determine the colour of the background of he form
depending on the position of the scroll button.
Use the command button named Exit to stop the program
Have a command button named Go that does absolutely nothing
Procedure
1. Open your project and add a New form. Set the following properties:
Name as 'frmControls'
Caption as 'Using Controls'
2. The plan of your layout should look like in the Figure below.
3. Set the properties for the controls by filling in the table below
Form 1
Colours
Red
Green
Blue
Add
List1
Delete
Clear
Text 2
Picture GO Exit
4. Click on the controls and add the necessary codes for the click Events: A few of the
codes are given below. Complete the code design.
Entry1.Text = List1.ListCount
End Sub
--------------
--------------
End Sub