1 / 18

Visual Basic Project 3

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.

holland
Télécharger la présentation

Visual Basic Project 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic Project 3

  2. Let’s build this form

  3. After clicking the image, typing new text and selecting a radiobutton

  4. 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.

  5. 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).

  6. 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

  7. 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.

  8. 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.

  9. 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.

  10. Setting the image

  11. 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.

  12. 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

  13. 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

  14. 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

  15. 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.

  16. 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.

  17. 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.

  18. 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.

More Related