160 likes | 302 Views
VB Controls and Events. Week 7: Picture Box, Image Box, Option, Check box, Mouse over, Frames, Shapes. PictureBox Label TextBox Frame CommandButton CheckBox OptionButton ComboBox ListBox. HScrollBar VScrollBar Timer DriveListBox DirListBox FileListBox Shape Line Image ….
E N D
VB Controls and Events Week 7: Picture Box, Image Box, Option, Check box, Mouse over, Frames, Shapes
PictureBox Label TextBox Frame CommandButton CheckBox OptionButton ComboBox ListBox HScrollBar VScrollBar Timer DriveListBox DirListBox FileListBox Shape Line Image … Controls
Both controls have "Picture" property which specifies an image file. Image is simpler (has fewer properties) Has Stretch property allowing it to be scaled PictureBox is more complex Picture show is actual size. If you want to scale it, use paint, and then display. PictureBox and Image
CheckBox • CheckBox has a • Caption • Value (True of False) • The value can be examined in an if-then statement, and the program can perform different actions based on the value. • The value can be set by the program, which will cause the check to appear or disappear.
Make a checkbox Set its caption Set its value Double click it to write code Run it and see how it works CheckBox values are 1 if checked and 0 if not. Private Sub Check1_Click() If Check1.Value = 1 Then Form1.BackColor = vbBlue Else Form1.BackColor = vbGreen End If End Sub Sample CheckBox Code
Option Buttons • Are always more than one, but one and only one of them can be on at any one time. • Each option button has • Caption • Value (True for only one option, and False for all others) • If you make one value True, others are automatically made False.
Make four Option Buttons Make the captions Freshman Sophomore Junior Senior and more Important: Think if the options cover all possibilities, and no two are possible at the same time. Senior or more is needed to cover 5th year students. Add the following code, run it and see what happens. Option values are True if the option is selected and false if not. Private Sub Option1_Click() PrintOptions End Sub Private Sub Option2_Click() PrintOptions End Sub Private Sub Option3_Click() PrintOptions End Sub Private Sub Option4_Click() PrintOptions End Sub Private Sub PrintOptions() Form1.Print Option1.Value; Form1.Print Option1.Value; Form1.Print Option1.Value; Form1.Print Option1.Value; End Sub Sample Option Button Code
If there are several different sets of options, each set of options must be in a frame (which may be invisible if you prefer) Only one option button in a frame can be True (selected) at any one time. Make the frame first, and put options in it later. Frames can be used, even if you don't use option buttons. Frames are useful for looks, and for grouping controls. Frame
Make two frames with caption "Year" and caption "Major" In the first frame put four option buttons as before In second, put CS/CMPE, Math, Econ, Accounting, SS, Law Note that the second option box does not allow double majors. If this was a real program we would need to Either list all double majors as options Or make checkboxes for majors Run without code Frame Sample Design
Put a shape control on the form It has properties of Shape FillColor FillStyle BorderColor BorderStyle BorderWidth Try changing these properties and see what happens. You can change these properties from your program as well. Shape
Understand the difference between using shapes and drawing on the form. A shape can be moved (simply by changing its Top, Left properties), a drawing can not. A shape can be resized by changing its Width and Height properties. A shape can be recolored, or even reshaped! Shape and Drawing
In the code window, click the top left drop-down box, and select Form Now click the top right drop-down box and see the options available. You can write a sub to respond whenever any of these "events" happens. We will write sample code to respond to "MouseMove", so select MouseMove from the right drop-down box. More Events
Sample Event Design • Make a shape • Set Shape property to Circle • Set FillStyle to Solid • Set FillColor to Red • Make a check box • Set caption to "Push/Pull" • Do as instructed in previous slide to get Sub Form_MouseMove(...) in your code window • Now add the code of the next slide.
Dim jump Private Sub Check1_Click() If Check1 = 1 Then jump = -8 Else jump = 8 End If End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Shape1.Top > Y Then Shape1.Top = Shape1.Top + jump Else Shape1.Top = Shape1.Top - jump End If If Shape1.Left > X Then Shape1.Left = Shape1.Left + jump Else Shape1.Left = Shape1.Left - jump End If End Sub Run the program, and check the checkbox. Move the cursor a lot. Now uncheck the checkbox. Move the cursor some more. Sample Event Code