14
VB Controls and Events Week 7: Picture Box, Image Box, Option, Check box, Mouse over, Frames, Shapes

VB Controls and Events

Embed Size (px)

DESCRIPTION

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 …. - PowerPoint PPT Presentation

Citation preview

Page 1: VB Controls and Events

VB Controls and Events

Week 7: Picture Box, Image Box, Option, Check box, Mouse over,

Frames, Shapes

Page 2: VB Controls and Events

Controls

• PictureBox• Label• TextBox• Frame• CommandButton• CheckBox• OptionButton• ComboBox• ListBox

• HScrollBar• VScrollBar• Timer• DriveListBox• DirListBox• FileListBox• Shape• Line• Image …

Page 3: VB Controls and Events

PictureBox and Image

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

Page 4: VB Controls and Events

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.

Page 5: VB Controls and Events

Sample CheckBox Code

• 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 IfEnd Sub

Page 6: VB Controls and Events

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.

Page 7: VB Controls and Events

Sample Option Button Code• 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() PrintOptionsEnd SubPrivate Sub Option2_Click() PrintOptionsEnd SubPrivate Sub Option3_Click() PrintOptionsEnd SubPrivate Sub Option4_Click() PrintOptionsEnd SubPrivate Sub PrintOptions() Form1.Print Option1.Value; Form1.Print Option1.Value; Form1.Print Option1.Value; Form1.Print Option1.Value;End Sub

Page 8: VB Controls and Events

Frame

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

Page 9: VB Controls and Events

Frame Sample Design

• 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

Page 10: VB Controls and Events

Shape

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

Page 11: VB Controls and Events

Shape and Drawing

• 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!

Page 12: VB Controls and Events

More Events

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

Page 13: VB Controls and 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.

Page 14: VB Controls and Events

Sample Event CodeDim jump

Private Sub Check1_Click() If Check1 = 1 Then jump = -8 Else jump = 8 End IfEnd 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 IfEnd Sub

• Run the program, and check the checkbox.

• Move the cursor a lot.• Now uncheck the

checkbox.• Move the cursor

some more.