Week 9 · Unit 2 Topic 2: AND gate simulation project with VB.NET: purpose, stages, controls, code in three parts · 3 lessons
2
Designing the simulator window: controls and what each does
ICT Secondary 3 Term 1, Unit 2, Topic 2: pp. 19–31
GoalList the controls of the simulator window and the properties set at the start, and say what the form-load event does.
New words
button · a control that is clicked; here each button acts as a switchText property · the words a control shows, here Off or OnForm Load · the event that runs once, when the window opensClick event · the event that runs each time its control is clicked
Explanation
The window needs only a few controls. Two buttons are the switches, named SwitchA and SwitchB. A third control, LampBox, is the lamp. In the original project the lamp is a picture box that shows a lit-lamp picture or an unlit-lamp picture; in our version we change the colour of a box, which is simpler and needs no picture files. A label may explain the window. Each control has properties: the Text of each switch is Off at the start. Properties can be set in the designer or in code, and both give the same result; code has one advantage: the start state is written down and is the same every time. The Form Load event runs once, when the window opens. It sets both texts to Off and shows the unlit lamp. After that the program waits for clicks, and each click runs the Click event of its button.
Control
Name
Job
button
SwitchA
the first switch: a click flips it
button
SwitchB
the second switch: a click flips it
box
LampBox
the lamp: gold when lit, gray when unlit
Worked examples
Control
Property
Start value
SwitchA
Text
Off
SwitchB
Text
Off
LampBox
BackColor
Gray (unlit)
the window
Text
AND gate simulator
The properties set at the start.
Vb
1Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
2 SwitchA.Text = "Off"3 SwitchB.Text = "Off"4 ShowLamp(False)
5End Sub
67Private Sub ShowLamp(lit As Boolean)
8 If lit Then
9 LampBox.BackColor = Color.Gold
10 Else
11 LampBox.BackColor = Color.Gray
12 End If
13End Sub
Output
Off Off -> unlit
The load event, with a small helper that shows the lamp. The output line is the state after the window loads: the text of A, the text of B, and the lamp.
Event
Belongs to
Runs when
Load
the window
the window opens
Click
SwitchA
A is clicked
Click
SwitchB
B is clicked
Which event runs when?
Class activity
Design on paper the window for the three-switch simulator of the last lesson. Name every control, give the start Text of each switch and the start look of the lamp, and list the events you will need.
Practice
1
How many Click events does the AND simulator have?
Two, one for each switch.
2
Which event sets the start state?
The Form Load event.
3
What is the start Text of each switch?
Off.
4
Give one advantage of setting the start state in code.
The start state is written down, so it is the same every time the window opens.