IMS 3253: Forms, Controls, Properties, Events 1 Dr. Lawrence West, MIS Dept., University of Central...

Post on 19-Jan-2018

215 views 0 download

description

IMS 3253: Forms, Controls, Properties, Events 3 Dr. Lawrence West, MIS Dept., University of Central Florida Visual Studio & Programming (cont.) Most 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 –Many other tasks

transcript

IMS 3253: Forms, Controls, Properties, Events

1Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Topics

• Program Elements• Assignment Operations• Events & Event Procedures• Properties• Tab Order and Access Keys

“Foolproof systems don’t take into account the ingenuity of fools”

Gene Brown

IMS 3253: Forms, Controls, Properties, Events

2Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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 (and generally incomprehensible to all but the most sophisticated developers)

IMS 3253: Forms, Controls, Properties, Events

3Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Visual Studio & Programming (cont.)

• Most 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– Many other tasks

IMS 3253: Forms, Controls, Properties, Events

4Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Projects

• Projects may consist of many parts– Solution—multiple programs

developed together• We will not use this

– Project—one program• All elements

are optional…• …but it must

have at least one

SOLUTION

PROJECT(Program)

FORM CLASS MODULE MISC.

IMS 3253: Forms, Controls, Properties, Events

5Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Projects—Forms

• Forms provide the visual interface for a program

• Forms contain– Controls– Properties– Methods– Events with code– Non-event code

• Controls also contain Properties, Methods, and Event Code

IMS 3253: Forms, Controls, Properties, Events

6Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Assignment Operations (Our First Code)

• A common code involves assigning a value to a container

• Both Container and Value can be– Property– Variable– Object

• The value portion can also be a literal• In this use the equal sign is the assignment

operator

Container = Value

IMS 3253: Forms, Controls, Properties, Events

7Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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 = stLastNameplaces the contents of the variable stLastName (whatever they are) into the Text property of the label control lblLastName

IMS 3253: Forms, Controls, Properties, Events

8Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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

IMS 3253: Forms, Controls, Properties, Events

9Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Events (cont.)

• Our first event—The 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

IMS 3253: Forms, Controls, Properties, Events

10Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Events (cont.)

• 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

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

IMS 3253: Forms, Controls, Properties, Events

11Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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

IMS 3253: Forms, Controls, Properties, Events

12Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Creating Event Procedures—Other Events

• 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

IMS 3253: Forms, Controls, Properties, Events

13Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Creating Event Procedures—Other Events

• The right side of the code window contains a list of all events supported by the form/control selected in the left box

• Selecting an event will create the event code template for that control and event combination

Selected Object (left side)

Supported Events (right side)

IMS 3253: Forms, Controls, Properties, Events

14Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Creating Event Procedures—Other Events

• There can be a dizzying variety of events supported for most objects

• Most are for events so obscure that you would never use them

• Some are incredibly useful and you should learn these over time

IMS 3253: Forms, Controls, Properties, Events

15Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Event Procedures—Multiple Events

• You will often want the same code to run for multiple events and/or controls

• Add the control/event combination to the Handles expression

Private Sub txtTransfer_GotFocus … Handles txtTransfer.GotFocus, _ txtTransfer.Click, txtTest.GotFocus, txtTest.Click

<Event Code>

End Sub

IMS 3253: Forms, Controls, Properties, Events

16Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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

'* Select the text in the text box theTextBox.SelectionStart = 0 theTextBox.SelectionLength = theTextBox.Text.Length

End Sub

IMS 3253: Forms, Controls, Properties, Events

17Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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

IMS 3253: Forms, Controls, Properties, Events

18Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Design Time Properties

• Selecting an object in formdesign mode gives access to the object’s properties in theproperties window

• These properties will be thedefault properties when a formis first created– Changes implemented in code

or by user interaction will persistas long as the form is open oruntil they are changed again

IMS 3253: Forms, Controls, Properties, Events

19Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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 must rename your object to indicate its:– Type (use a prefix)– Purpose of use (rest of the name)

• You need not rename labels that are not addressed in code

IMS 3253: Forms, Controls, Properties, Events

20Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Name Property (cont.)

• Examples:– lblLastName label– btnCancel button– txtStreetAddress text box– dtpBirthDate date time picker– cboStyle combo box– lstDepartment list box– chkActive check box– rdoMaritalStatus radio button– CustomerForm form

IMS 3253: Forms, Controls, Properties, Events

21Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Important Form Properties

• Accept Button—button whose code will execute if the Enter key is pressed

• Cancel Button—button whose code will execute if the Escape key is pressed

• Text*—displays in the title bar• ControlBox*, FormBorderStyle, Maximize* &

Minimize* Buttons• StartPosition—where form will initially load• Size—Width & Height of the form

* Removing these will give a form with no title bar

IMS 3253: Forms, Controls, Properties, Events

22Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Common Control Properties

• Text—what is displayed in the control• Left / Top—coordinates in pixels from the upper

left corner of the form for the location of the upper left corner of the control

• Width / Height—size in pixels of the control• Enabled—Control is always visible but can be

toggled to active or inactive & dimmed• Visible—Control is visible or not• TabIndex—sequence of the control in the form’s

tab order

IMS 3253: Forms, Controls, Properties, Events

23Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Key Interaction Control Value Properties

• Text Box—Text• Check Box—Checked• Radio Button—Checked• Combo Box & List Box—Text, SelectedIndex,

SelectedValue, SelectedText• Date Time Picker—Value

IMS 3253: Forms, Controls, Properties, Events

24Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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

IMS 3253: Forms, Controls, Properties, Events

25Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

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

IMS 3253: Forms, Controls, Properties, Events

26Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Getting Stupid with Properties

• You can be 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

IMS 3253: Forms, Controls, Properties, Events

27Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Setting Tab Order

• After the form is complete there is an easy to set the whole form’s 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

IMS 3253: Forms, Controls, Properties, Events

28Dr. Lawrence West, MIS Dept., University of Central Floridalwest@bus.ucf.edu

Setting Access Keys

• Putting an “&” in the Text property of a control makes the following letter the hot key for the control.

– Gives control focus if Alt-Letter is pressed– Executes button if it is a button’s Access key

• Text boxes have no permanent Text property– Set the hot key in a label that immediately preceds

the text box in the form’s tab order– Labels cannot get focus but focus will shift to the

text box