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.

located at (100, 100). By default, the size of this box is about 200×80. Let’s<br />

make this box wider by calling the SetSize() method. Add this line of code<br />

just after creating the multiline text box:<br />

Controls.SetSize(msgText, 280, 80) ' Makes width = 280 and height = 80<br />

The first argument is the identifier of the control you want <strong>to</strong> resize,<br />

in this case msgText. The second argument (280) is the width, and the third<br />

(80) is the height. If you run the code now, you’ll see an interface similar <strong>to</strong><br />

the one shown earlier in Figure 12-2. Note that the upper-left corner of the<br />

message text box didn’t change when you called SetSize().<br />

Step 2: <strong>Program</strong> Interactivity<br />

You’ve created all the controls you need and positioned them where you<br />

wanted them. Next, you’ll make these controls interactive. You need <strong>to</strong><br />

write some code that responds <strong>to</strong> the but<strong>to</strong>n’s click. When a user clicks the<br />

but<strong>to</strong>n, the program needs <strong>to</strong> read the contents of the first name and the<br />

last name text boxes and then display the greeting in the multiline text box.<br />

Add lines 13–21, as shown in Listing 12-1, <strong>to</strong> complete the program (you’ve<br />

already written lines 2–11 <strong>to</strong> create the GUI elements).<br />

1 ' FirstGUIApp.sb<br />

2 GraphicsWindow.DrawText(20, 20, "First name:") ' Label<br />

3 fnText = Controls.AddTextBox(100, 20) ' First name text box<br />

4<br />

5 GraphicsWindow.DrawText(20, 60, "Last name:") ' Label<br />

6 lnText = Controls.AddTextBox(100, 60) ' Last name text box<br />

7<br />

8 showBtn = Controls.AddBut<strong>to</strong>n("Show Message", 280, 20) ' But<strong>to</strong>n<br />

9<br />

10 msgText = Controls.AddMultiLineTextBox(100, 100) ' Message text box<br />

11 Controls.SetSize(msgText, 280, 80) ' Makes width = 280 and height = 80<br />

12<br />

13 Controls.But<strong>to</strong>nClicked = OnBut<strong>to</strong>nClicked ' Handler for but<strong>to</strong>n click<br />

14<br />

15 Sub OnBut<strong>to</strong>nClicked<br />

16 firstName = Controls.GetTextBoxText(fnText) ' First name text box<br />

17 lastName = Controls.GetTextBoxText(lnText) ' Last name text box<br />

18 fullName = firstName + " " + lastName ' Constructs full name<br />

19 message = "Hello there, " + fullName + "!" ' Greeting message<br />

20 Controls.SetTextBoxText(msgText, message)<br />

21 EndSub<br />

Listing 12-1: Creating a simple GUI program<br />

Line 13 registers a handler for the But<strong>to</strong>nClicked event. This line tells<br />

the Controls object <strong>to</strong> call the OnBut<strong>to</strong>nClicked() subroutine whenever the<br />

user clicks the Show Message but<strong>to</strong>n.<br />

In the OnBut<strong>to</strong>nClicked() subroutine, GetTextBoxText() is called first <strong>to</strong><br />

get the text that’s entered in<strong>to</strong> the first-name text box and save it in<strong>to</strong> the<br />

168 Chapter 12

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

Saved successfully!

Ooh no, something went wrong!