180 likes | 269 Views
Visual Basic Project 3. Let’s build this form. After clicking the image, typing new text and selecting a radiobutton. What’s on the form?. You should recognize two labels and a textbox. Additionally, there are some new components.
E N D
After clicking the image, typing new text and selecting a radiobutton
What’s on the form? • You should recognize two labels and a textbox. • Additionally, there are some new components. • Notice, from screenshot, you can set font and color properties of labels and textbox text properties.
About the label • Default properties for label might be set to autosize true. If this is the case, select autosize and set it to false so you can resize the label. • Set the font to something – I used Symbol (Greek).
New components are groupbox, radiobutton and picturebox • From the toolbox: Select a groupbox and drop it on your form. It is a “box” thing for grouping other components, in this case, radiobuttons. • Now drag/drop three radiobuttons into the groupbox. • Drop a picturebox on your form. • And another picturebox, right on top of the first. • And the textbox and labels
Set properties • As usual, go through properties for each components and rename with VB style names. • use radXXX to name radioButtons. • Use pbXXX to name picturebox. • You may leave default names for controls which are not accessed in subroutines, though you’ll have to be careful • You should not generally need to reference groupboxes, or even labels, unless the text on them is used for result-display purposes. • I left my groupbox with its default name. • Name your labels and textbox and set their text properties.
Note about next step • You will need to have some image files saved in your p or w drives. • If you don’t have any, go find some now. • I used tomcat icon. I couldn’t find the fireguy icon I used, but there is a cute gif file called stickmanonfire you can search for/save and use.
properties • I set the font of my lblMessage to be symbol (greek). • For each picturebox, I selected the image property and then clicked the (…) to open a Select Resource Dialog Box, clicked import, and browsed my system to find some images. (See previous slide if you can’t find any images.) • See next slide for what this looks like.
What functionality do we want? • Radiobuttons should change the color of the displayed text (in lblMessage). • Since these are in a groupbox, VB will handle the exclusionary aspect of the functionality. • When one image is clicked, I want it to go away and the other to be displayed.
Subroutines needed Click on a radiobutton and provide code like lblMessage.ForeColor = Color.Green to the stubbed subroutine. Here’s a complete example. (Note that all the code for a single line must fit on one line): Private Sub rbGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGreen.CheckedChanged lblMessage.ForeColor = Color.Green End Sub
A problem with the code in the previous slide Green radio button “checked changed” event is fired when green radiobutton is selected or when it is unselected. The code in the previous slide works because, when blue (for example) is selected, first green rb checked changed event is fired, then blue rb checked changed is fired, so the user’s current choice is updated properly. An improved event handler would have code to check whether the rb is CURRENTLY checked with a control structure called if… then: If rbGreen.checked then lblMessage.ForeColor = Color.Green End if
More Subroutines needed • Click a picturebox. Add code like this to one stubbed method (and the opposite code to the other): PictureBox2.Visible = False PictureBox1.Visible = True • Here’s one of the two complete subroutines: Private Sub pbOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbOne.Click pbOne.Visible = False pbTwo.Visible = True End Sub
More Subroutines needed • Double-click in your textbox and a text-changed subroutine will open up. What should happen when the textbox text is altered? You should copy that text into the label’s text property.
OOP: Classes and variable scope • Classes consist of fields, constructors and methods. • In VB, methods are called subs. • Fields are variables declared at the top of the class, visible to all the subs and functions in the class. Such variables should not be redefined in a sub – they are already available. • We have already been using class fields, like txtinput.text and methods, like txtinput.clear() or me.close() in our examples.
variable scope • Parameters to functions and subs have local scope. They live only within the sub or function and are not available elsewhere. • VB defines your controls for you (in hidden code). They are fields of the class and have global scope. • Variables dimensioned inside subs and functions also have local scope and can’t be referenced outside the sub or function containing the definition.
variable scope • A variable which needs to be accessed in many subs will need to be passed to each, or declared as a class field.