19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You start by loading the two background images and saving their identifiers<br />

in the offImg and onImg variables (line 2–3). The bkgndImg variable<br />

holds the current background image, which changes when a user clicks<br />

the switch. When the program starts, the switch is open, so the program<br />

sets bkgndImg = offImg (line 4). Lines 6–7 adjust the width and height of the<br />

graphics window <strong>to</strong> match the size of the background image, and line 8<br />

draws the background image (offImg in this case) in the graphics window.<br />

Lines 10–17 create the four text boxes (for the three resis<strong>to</strong>rs and the<br />

battery voltage) and resize them so they’re exactly on <strong>to</strong>p of their positions<br />

in the background image. In lines 18–21, you set default values for these<br />

text boxes. In line 23, you register a handler for the MouseDown event, because<br />

you’ll want <strong>to</strong> know when a user clicks the switch. Line 24 registers a handler<br />

for the TextTyped event, because you’ll calculate and display the values<br />

of I, V1, V2, and V3 au<strong>to</strong>matically when the user enters a new value in any of<br />

the four text boxes.<br />

Step 3: Toggle the Switch<br />

When a user clicks the switch, you need <strong>to</strong> change the background image <strong>to</strong><br />

<strong>to</strong>ggle the switch. Add the OnMouseDown() subroutine in Listing 12-4.<br />

1 Sub OnMouseDown ' Switches the background image<br />

2 x = GraphicsWindow.MouseX<br />

3 y = GraphicsWindow.MouseY<br />

4 If ((x > 185) And (x < 245) And (y > 300) And (y < 340)) Then<br />

5 If (bkgndImg = offImg) Then<br />

6 bkgndImg = onImg<br />

7 Else<br />

8 bkgndImg = offImg<br />

9 EndIf<br />

10 UpdateUserInterface()<br />

11 EndIf<br />

12 EndSub<br />

Listing 12-4: Changing the background image<br />

The subroutine starts by getting the x- and y-coordinates of the point<br />

where the mouse was clicked and setting them <strong>to</strong> the variables x and y<br />

(lines 2–3). Line 4 then checks if this point lies <strong>with</strong>in the rectangular<br />

region of the switch; if the mouse was inside the boundaries of the switch,<br />

the subroutine <strong>to</strong>ggles the current value of the bkgndImg variable (from on<br />

<strong>to</strong> off or from off <strong>to</strong> on) at lines 5–9 and then calls the UpdateUserInterface()<br />

subroutine <strong>to</strong> switch the background image and update the calculated<br />

values (line 10). As you’ll see in a moment, if the user opens the switch,<br />

the program shows only the offImg background image; the values of I, V1,<br />

V2, and V3 won’t show because no current’s flowing through the circuit when<br />

the switch is open.<br />

174 Chapter 12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!