Business programming chapter 2
Visual Basic, Controls, and Events
Visual Basic
• Language used to create Windows applications.
• Provides a Graphical User Interface or GUI.
• The instructions executed in the program is controlled by events.
How You Develop a Visual Basic Program
• Design the Interface for the user.
• Determine which events the controls on the window should recognize.
• Write the event procedures for those events.
4 Ways to Place a Control from the Toolbox onto the Form Designer
• Double-click
• Drag and Drop
• Click, Point, and Click
• Click, Point, and Drag
Text Box Control
• Used for input and output
• When used for output, Read Only property is set to True
Some Often Used Properties
• Text
• Autosize
• Font.Name
• Font.Size
• ForeColor
• BackColor
• ReadOnly
Setting Properties
• Click on property name in left column.
• Enter its setting into right column by typing or selecting from options displayed
via a button or ellipsis
Button Control
• The caption on the button should indicate the effect of clicking on the button.
Label Control
• Used to identify the contents of a text box
• Text property specifies caption
• By default, label automatically resizes to accommodate caption on one line.
• When the AutoSize property is set to False, label can be resized manually.
AutoSize is used primarily to obtain a multi-rowed label.
List Box Control
• Initially used to display several pieces of output.
• In Chapter 4 used to select from a list.
The Name Property
• Used by the programmer to refer to a control in code
• Setting for Name property near top of Properties window
• Use appropriate 3-character naming prefix
• Use descriptive names
Fonts
• Proportional width fonts, such as Microsoft Sans Serif, use less space for “I” than
for “W”.
• Fixed-width fonts, such as Courier New, take up the same amount of space for
each character.
• Fixed-width fonts are used for tables
Auto Hide
• Hides Toolbox when not in use
• Vertical push pin icon indicates auto hide is disabled.
• Click the push pin to make it horizontal and enable auto hide.
Tab Order
• The tab indices determine the order in which controls receive the focus during
tabbing.
• The control whose TabIndex property is set to 0 has the focus when the program
begins.
Event
• An event is an action, such as the user clicking on a button
• Usually, nothing happens in a Visual Basic program until the user does something
and raises an event.
• What happens is determined by statements inside the event procedure
Focus
• When you click on a text box, a cursor appears in the text box, and you can type
into the text box.
• Such a text box is said to have the focus.
• If you click on another text box, the first text box loses the focus and the second
text box receives the focus
Sample Statements
• txtBox.ForeColor = Color.Red
• txtBox.Visible = True
• txtBox.Text = "Hello World"
Examples of Events
• btnShow.Click
• txtBox.TextChanged
• txtBox.Leave
The Three Steps in Creating a Visual Basic Program
1. Create the interface; that is, generate, position, and size the objects.
2. Set properties; that is, configure the appearance of the objects.
3. Write the code that executes when events occur
Display Events for a Control
• Select the control
• Click on the Events button in the Properties window
Create an Outline for an Event Procedure
• Double-click on a control or
• Select a control, click on the Events button in the Properties window, and double-
click on an event (We nearly always use the first method.)
Sample Form
Double-click on txtFirst to create the outline for the Code Editor
Sample Form
Double-click on btnRed to return to Code Editor and add the outline of an event
procedure.
Event Procedure txtFirst.Leave
• Select txtFirst on the form
• Click on the Events button in the Properties window
• Double-click on Leave