ISM 3253 Topics 2: Form, Control, Event
ISM 3253 Topics 2: Form, Control, Event
ISM 3253 Topics 2: Form, Control, Event
Event
Program Elements
Assignment Operations
Events & Event Procedures
Properties
Tab Order and Access Keys
Save Files
Review: Visual Studio &
Programming
A computer program consists of instructions
readable by the computer which direct its
operations
In modern development, programs begin as
instructions readable by (trained) humans…
…And end up as instructions readable by the
computer
Review: Visual Studio &
Programming (cont.)
Programs consist of many parts …
Visual Studio and Visual Basic support:
Authoring the component parts of the programs
Managing the program parts
Converting the parts you write into machine readable
format
Combining the parts you write with a huge collection
of pre-written instructions
Other tasks
Projects
All elements
are optional… FORM CLASS MODULE MISC.
…but it must
have at least
one
Projects—Forms
Examples
intQuantity = 123
places the value 123 into the variable intQuantity
stLastName = “Jones”
places the value “Jones” into the variable stLastName
lblLastName.Text = stLastName
places the contents of the variable stLastName
(whatever they are) into the Text property of the label
control lblLastName
Events
Forms, Controls, and Classes recognize events
Events are predefined (or programmer defined)
actions against an object
If the action occurs…
And if there is code written for the event…
Then the code will execute
Three kinds of events for forms and controls
Commonly used
Less commonly used
Almost never used
Events (cont.)
Our first event—The Click Event for a button
lblTransfer txtTransfer cmdTransfer
lblTransfer.Text = txtTransfer.Text
End Sub
Events (cont.)
Line continuation character
Name of the event procedure
Event Arguments
Event Arguments are created automatically and are only used for advanced
purposes
The procedure name and Handles expression are also created automatically
The event procedure header (what we are seeing here) is actually created
as one long line
Use the line continuation character to break it into two physical lines
But still treated as one logical line
Creating Event Procedures—
Default Events
Double-clicking a control or form in design view
creates the template for the default event
Form: Load event
Button: Click event
Text Box: TextChanged event
Label: Click event
Check Box & Radio Button: CheckChanged event
Combo Box & List Box: SelectedIndexChanged
Date Time Picker: ValueChanged event
NumericUpDown: ValueChanged event
Creating Event Procedures—
Other Events
The at the top left corner of the code window
is a drop-down list of all objects on the form
Select the object whose event you want to program
Creating Event Procedures—
Other Events
The right side of the code window contains a list of all
events supported by form/control selected in the left box
Selected Object (left side)
<Event Code>
End Sub
Event Procedures—Multiple
Events (cont.)
Private Sub txtTransfer_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtTransfer.GotFocus, _
txtTransfer.Click, txtTest.GotFocus, txtTest.Click
'***************************************************************
'* Handles the GotFocus and Click events of all text boxes on
'* the form.
'* Automatically selects the contents of the text box so that
'* text can be replaced by typing
'***************************************************************
'* Create a text box object
Dim theTextBox As TextBox
'* Set the object to the control that triggered the event
theTextBox = sender
End Sub
Properties
Visual design elements (forms and controls)
have properties that
Control their behavior
Determine their appearance
Interact with the user
Properties may be set at…
Design time
Run time
In code
With user interaction
Both Design & Run Time
Design Time Properties
Selecting an object in form
design mode gives access to
the object’s properties in the
properties window
These properties will be the
default properties when a form
is first created
Changes implemented in code
or by user interaction will persist
as long as the form is open or
until they are changed again
Name Property
lblTransfer.Text = txtTransfer.Text
End Sub
Properties in Code
Private Sub btnCatchMe_MouseMove(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnCatchMe.MouseMove
'***************************************************************
'* What is going on here????
'***************************************************************
If Now.Second < 55 Then
If Now.Second < 30 Then
btnCatchMe.Top = 20
Else
btnCatchMe.Top = Me.Height - btnCatchMe.Height - 30
End If
If Now.Second Mod 2 = 1 Then
btnCatchMe.Left = 20
Else
btnCatchMe.Left = Me.Width - btnCatchMe.Width - 20
End If
End If
End Sub
Getting Stupid with Properties
End Sub
The Solution Explorer as a form file is being
renamed
How to rename a file, project, or solution
Right-click on it in the Solution Explorer window and select the
Rename command from the shortcut menu. Or, select it in the
Solution Explorer and press F2. Then, you can enter the new
name.
Be sure NOT to change or omit the file extension when you
rename a file.
Remember too that using a three-letter prefix to indicate the
contents of the file (like frm for a form file) makes it easier to
tell what each file represents.
When you change the name of a form file, Visual Studio will
also change the Name property for the form and update any
references within the existing code for the form.
How to save a file, project, or solution
You can use the Save All button in the Standard toolbar or the
Save All command in the File menu to save all files and projects
in the solution.
You can use the Save button in the Standard toolbar or the Save
command in the File menu to save a file, project, or solution. The
files that are saved depend on what’s selected in the Solution
Explorer window.
If a single file is selected, just that file is saved.
If a project is selected, the entire project and its solution are
saved.
If a solution is selected, the entire solution and all its projects
are saved.
If you try to close a solution that contains modified files, a dialog
box is displayed that asks you if you want to save those files.