of 35
8/8/2019 03 Projects Lecture2
1/35
8/8/2019 03 Projects Lecture2
2/35
Review: Visual Studio &
Programming
A computerprogram 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
8/8/2019 03 Projects Lecture2
3/35
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 ofpre-written instructions
Other tasks
8/8/2019 03 Projects Lecture2
4/35
Projects
Projects may consist of many parts
Solutionmultiple programs
developed together
We will not use this
Projectone program
All elements
are optional
but it must
have at least
one
SOLUTION
PROJECT
(Program)
FORM CLASS MODULE MISC.
8/8/2019 03 Projects Lecture2
5/35
ProjectsForms
Forms provide the
visualinterface for a
program Forms contain
Controls
Properties
Methods Events with code
Non-event code
Controls also contain Properties, Methods,
and Event Code
8/8/2019 03 Projects Lecture2
6/35
8/8/2019 03 Projects Lecture2
7/35
Assignment Operations (cont.)
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
8/8/2019 03 Projects Lecture2
8/35
Events
Forms, Controls, and Classes recognize
events
Events are predefined (or programmer
defined) actions against an object Ifthe action occurs
Andifthereis code written fortheevent
Thenthe code willexecute
Three kinds of events for forms and controls Commonly used
Less commonly used
Almost never used
8/8/2019 03 Projects Lecture2
9/35
Events (cont.)
Our first eventThe Click Event for a button
Private Sub cmdTransfer_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdTransfer.Click
'***************************************************************
'* Places the contents of the txtTransfer text box
'* into the lblTransfer label text property'***************************************************************
lblTransfer.Text = txtTransfer.Text
End Sub
cmdTransfertxtTransferlblTransfer
8/8/2019 03 Projects Lecture2
10/35
Events (cont.)
Event Arguments are created automatically and are only used for
advanced purposes
The procedure name and Handles expression are also createdautomatically
The event procedure header (what we are seeing here) is actually
created as one longline
Use the line continuation character to break it into twophysicallines
But still treated as one logicalline
Private Sub cmdTransfer_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdTransfer.Click
Name of the event procedureLine continuation character
Event Arguments
Control and event that triggers the code
8/8/2019 03 Projects Lecture2
11/35
Creating Event Procedures
Default Events Double-clickinga 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
8/8/2019 03 Projects Lecture2
12/35
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
8/8/2019 03 Projects Lecture2
13/35
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
Selecting an event will create the event codetemplate for that control and event combination
Selected Object (left side)
Supported Events (right side)
8/8/2019 03 Projects Lecture2
14/35
Creating Event Procedures
Other Events There can be a dizzying variety of events
supported for most objects
Most are for events so obscure that youwould never use them
Some are incredibly useful and you should
learn these over time
8/8/2019 03 Projects Lecture2
15/35
Event ProceduresMultiple
Events You will often want the same code to run for
multiple events and/or controls
Add the control/event combination to theHandles expression
Private Sub txtTransfer_GotFocus
Handles txtTransfer.GotFocus, _
txtTransfer.Click, txtTest.GotFocus, txtTest.Click
End Sub
8/8/2019 03 Projects Lecture2
16/35
Event ProceduresMultiple
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
'* Select the text in the text box
theTextBox.SelectionStart = 0
theTextBox.SelectionLength = theTextBox.Text.Length
End Sub
8/8/2019 03 Projects Lecture2
17/35
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
8/8/2019 03 Projects Lecture2
18/35
Design Time Properties
Selecting an object in form
design mode gives access to
the objects properties in theproperties 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
8/8/2019 03 Projects Lecture2
19/35
Name Property
Every object has a Name property that
controls how your object is addressed in code
If the object is addressed in code you mustrename your object to indicate its:
Type (use a prefix)
Purpose of use (rest of the name) --
(e.g.txtTransfer) first-letter Capital
Youneednot renamelabels thatarenot
addressedon code
8/8/2019 03 Projects Lecture2
20/35
Name Property (cont.)
Examples: lblLastName label
btnCancel or cmdCancel button
txtStreetAddress text box
dtpBirthDate date time picker
cboStyle combo box
lstDepartment list box
chkActive check box
rdoMaritalStatus radio button
frmCustomer form
8/8/2019 03 Projects Lecture2
21/35
Important Form Properties
Accept Buttonbutton whose code will
execute if the Enter key is pressed
Cancel Buttonbutton whose code willexecute if the Escape key is pressed
Text*displays in the title bar
ControlBox*, FormBorderStyle, Maximize* &
Minimize* Buttons
StartPositionwhere form will initially load
SizeWidth & Height of the form
* Removing these will give a form with no title bar
8/8/2019 03 Projects Lecture2
22/35
Common Control Properties
Textwhat is displayed in the control
Left / Topcoordinates in pixels from the
upper left corner of the form for the location
of the upper left corner of the control Width / Heightsize in pixels of the control
EnabledControl is always visible but can be
toggled to active or inactive & dimmed VisibleControl is visible or not
TabIndexsequence of the control in the
forms tab order
8/8/2019 03 Projects Lecture2
23/35
8/8/2019 03 Projects Lecture2
24/35
Reading & Setting Properties
in Code
Properties may be read and set in code by
referring to them on the appropriate side of
an assignment operator (equal sign)
Private Sub cmdTransfer_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdTransfer.Click
'***************************************************************
'* Places the contents of the txtTransfer text box
'* into the lblTransfer label text property
'***************************************************************
lblTransfer.Text = txtTransfer.Text
End Sub
8/8/2019 03 Projects Lecture2
25/35
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 = 20Else
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
8/8/2019 03 Projects Lecture2
26/35
Getting Stupid with Properties
You can do VERY stupid with appearance
properties
BackColor ForeColor
Font
Experiment and get it out of your system
Then stick with the defaults for your work in
this class
8/8/2019 03 Projects Lecture2
27/35
Setting Tab Order
After the form is complete there is an easy to
set the whole forms tab order sequence
Select View | Tab Order from the menu
Click box by each control in the tab order you
want
If you screw it up start over
Set individual control Tab Stop properties to False
to exclude from the tab order
8/8/2019 03 Projects Lecture2
28/35
Setting Access Keys
Putting an & in the Text property of a control
makes the followingletter the hot key for the
control.
Gives control focus if Alt-Letter is pressed
Executes button if it is a buttons Access key
Text boxes have no permanent Text property Set the hot key in a label that immediately
precedes the text box in the forms tab order
Labels cannot get focus but focus will shift to the
text box
8/8/2019 03 Projects Lecture2
29/35
The Invoice Total form
Three types of controls
y A labeldisplays text on a form.
y A textbox lets the user enter text on a form.
y A button initiates form processing when clicked.
8/8/2019 03 Projects Lecture2
30/35
A form after some controls have been added to it
8/8/2019 03 Projects Lecture2
31/35
A form after the properties have been set
8/8/2019 03 Projects Lecture2
32/35
Event ProceduresMultiple
Events
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
'* Select the text in the text box
theTextBox.SelectionStart = 0
theTextBox.SelectionLength = theTextBox.Text.Length
End Sub
8/8/2019 03 Projects Lecture2
33/35
The Solution Explorer as a form file is being
renamed
8/8/2019 03 Projects Lecture2
34/35
How to rename a file, project, or solution
y Right-clickon it in the Solution Explorer window and select theRename command from the shortcut menu. Or, select it in theSolution Explorer and press F2. Then, you can enter the new
name.
y Be sure NOT to change or omit the file extension when yourename a file.
y Remembertoothatusingathree-letterprefixtoindicatethecontentsofthefile (likefrmforaformfile)makesiteasierto
tellwhateachfilerepresents.
y Whenyouchangethenameofaformfile, VisualStudiowillalsochangetheNamepropertyfortheformandupdateanyreferenceswithintheexistingcodefortheform.
8/8/2019 03 Projects Lecture2
35/35
How to save a file, project, or solution
y You can use the Save All button in the Standard toolbar or theSave All command in the File menu to save all files and projects
in the solution.
y You can use the Save button in the Standard toolbar or the Savecommand in the File menu to save a file, project, or solution. The
files that are saved depend on whats selected in the Solution
Explorer window.
y If a single file is selected, just that file is saved.y If a project is selected, the entire project and its solution are
saved.
y If a solution is selected, the entire solution and all its projectsare saved.
y If you try to close a solution that contains modified files, a dialogbox is displayed that asks you if you want to save those files .