☰ Year plan · Communication Technology

The AND simulator in VB.NET

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.

ControlNameJob
buttonSwitchAthe first switch: a click flips it
buttonSwitchBthe second switch: a click flips it
boxLampBoxthe lamp: gold when lit, gray when unlit
Worked examples
ControlPropertyStart value
SwitchATextOff
SwitchBTextOff
LampBoxBackColorGray (unlit)
the windowTextAND 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
6
7Private 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.
EventBelongs toRuns when
Loadthe windowthe window opens
ClickSwitchAA is clicked
ClickSwitchBB 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?
2
Which event sets the start state?
3
What is the start Text of each switch?
4
Give one advantage of setting the start state in code.