General Information
Student Resources and Information
Interests
|
Listbox and Buttons in a Window
This project creates a window containing a list box and two buttons.
One of the buttons will cause the turtle to draw whatever shape has been selected in the listbox. The other button will close the window.
We are familiar with the windowcreate and buttoncreate commands.
The listboxcreate command takes fewer inputs. A listbox doesn't have a caption and there are no instructions executed at any stage. The required inputs in order are:
- The name of the parent window,
- the name of the list box,
- the x-coordinate of the top left hand corner (relative to the top left-hand corner of the parent window),
- the y-coordinate of the top left hand corner (relative to the top left-hand corner of the parent window),
- the width of the list box,
- the height of the listbox.
The command listboxaddstring "liosta [triangle] will add the word "triangle" to the listbox whose name is liosta
The command listboxgetselect "liosta outputs what is currently selected in the listbox whose name is liosta .
The command run listboxgetselect "liosta will run the procedure with the name that is output by listboxgetselect "liosta . This is the command that is executed below when the button with the caption "Draw" is pressed.
I have created a Java driven simulation below.
Type the word lbox into the text box below and press ENTER:
The procedures used are:
to lbox
windowcreate "main "fuinn [Shapes] 0 0 150 150 []     ;Create a window called fuinn.        
                                                                                ;Its title bar will have the word "Shapes" on it.
listboxcreate "fuinn "liosta 50 20 60 30     ;Create a listbox called liosta.        
                                                              ;within the window called "fuinn".
listboxaddstring "liosta [square]     ;Add the word "square" to the listbox.        
listboxaddstring "liosta [triangle]     ;Add the word "triangle" to the listbox.        
listboxaddstring "liosta [pentagon] ;Add the word "pentagon" to the listbox.        
buttoncreate "fuinn "cnaipe1 [Draw] 62 70 30 10 [run listboxgetselect "liosta]  ;Create a button called cnaipe1 within the window                                                                                 "fuinn".        
                                                                                ;This button will run whatever is selected from the listbox.
buttoncreate "fuinn "cnaipe2 [Cancel] 62 110 30 10 [windowdelete "main]
end
|
to triangle
repeat 3[fd 100 rt 120]
end |
to square
repeat 4 [fd 100 rt 90]
end |
to pentagon
repeat 5[fd 100 rt 72]
end |
|