CIS 115 Lecture 3. Forms Form properties Controls Control properties Event Driven Programming ...

35
CIS 115 Lecture 3

Transcript of CIS 115 Lecture 3. Forms Form properties Controls Control properties Event Driven Programming ...

Page 1: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

CIS 115 Lecture 3

Page 2: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

FormsForm propertiesControls Control propertiesEvent Driven ProgrammingForm EventsControl EventsEvent HandlersVB Example Program

Page 3: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

A form is a container for controlsA form is used to design a GUI-based

window in a Windows applicationA form displays information and

receives input from the user. Always orient a form at a task as

defined by the user

Page 4: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Text – defines the text to display in the caption bar

StartPosition – determines position of form when it first appears (eg. CenterScreen)

Size.Width, Size.Height – the 2D area occupied by the form, in units of pixels

Location.X, Location.Y – the relative position of the form on the screen

Visible – can be seen by the user Enabled – the user can interact with the form

Page 5: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

FormBorderStyle – determines the appearance and behavior of the borders of the form Sizable: (Default) Has min, max, and close buttons;

can be resized by dragging edges Fixed3D: Has a 3D look; min, max, and close

buttons; cannot be resized FixedSingle: Has single line border; min, max, and

close buttons; cannot be resized AcceptButton - designates which button on

the form is activated by the Enter Key Cancel Button - designates which button on

the form is activated by the ESC Key

Page 6: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Visual objects that are placed on a form to enable customized activities

Familiar Visual Basic controls: Label - displays text the user cannot change TextBox - allows the user to enter text Button – performs an action when clicked RadioButton - A round button that is selected or deselected with a

mouse CheckBox – A box that is checked or unchecked with a mouse click Form - A window that contains these controls

Built-in controls defined in Windows Form class library, and are defined with ToolBox and Form Designer or strictly with code

Page 7: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Text edit (TextBox—txt___) Text display (Label—default name or lbl___) Selection from a list (ListBox—lst___, ComboBox—

cbo___, ListView, TreeView, NumericUpDown…) Graphic display (PictureBox—pic___) Graphic storage (ImageList) Value setting (CheckBox—chk___, CheckListBox,

RadioButton,…) Date setting (DateTimePicker, MonthCalendar) Dialog boxes (OpenFileDialog, PrintDialog…) Menu controls (MainMenu, …) Commands (Button—btn___, LinkLabel…) Grouping other controls (GroupBox, TabControl,

Panel)

Page 8: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Common properties shared by many controls Name, Text Size.Height & Width, Location.X &Y, Dock BackColor: Sets the background (fill) color ForeColor: Sets the foreground (text) color CanFocus, ContainsFocus, Focused Visible & Enabled determine availability to user Font properties affect text display in the control

▪ Font, size, bold, etc. Tab Index & Tab Stop

Page 9: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Design Time Set in Properties Window

Run Time Set / Change in Code

Page 10: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Slide 2- 10

Specify the control name (btnExit)Then a dotThen the PropertyName (Visible)

controlName.propertyName btnExit.Visible

▪ refers to the Visible property of the btnExit control▪ The visible property values may only be true or false

Page 11: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Item to receive the value (Left Side)Assignment Indicator = Value to be assigned(Right Side)

VariableName = Value NumberVariable = 5

ControlName.PropertyName = Setting btnExit.Visible = False

▪ Assigns the value False to the Visible property of the btnExit control

▪ Causes the text of the btnExit control to become hidden to the user

txtFirstName.text = “Paul” txtLastName.text = “Overstreet”

Page 12: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Properties Text

▪ &Cancel -> Cancel▪ && -> &

Events Click

Page 13: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Use labels and link labels for text display Text property (no more Caption)

defines text to display User cannot change a label

LinkLabel enables hyperlinks Links.Add inserts a hyperlink into

text Must write event-handler to invoke

browser See example

Page 14: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Text box allows user to enter or edit data

Properties MaxLength, MultiLine AcceptsTab AcceptsReturn WordWrap ScrollBars

Events TextChanged

Page 15: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

CheckState property Checked Unchecked Indeterminate

(checked but grayed)

Text property displays built-in caption

If chkMarried.CheckState = CheckState.Checked Then End If

Page 16: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

ComboBox Properties Text DropDownStyle

▪ Simple▪ Dropdown▪ DropdownList

Sorted Methods

Items.Clear Items.Add Items.Remove

cboChoice.Items.Clear()cboChoice.Items.Add("First")cboChoice.Items.Add("Second")cboChoice.Items.Add("Third")cboChoice.Items.Add(TextBox1.Text)

cboChoice.Items.Remove("Third")

Page 17: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Executes code after a specified interval

Timer Event Unique event that executes

after the interval specified in the interval property expires

Interval Property 0 - 65,535 milliseconds

▪ 0 - means disabled▪ 60,000 milliseconds is one

minute Enabled property must

also be true for timer to work.

Timer control is never visible at run time

Stored in Component Tray at design time

Page 18: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Applications recognize and respond to events by executing code known as event procedures

Event: An action that is recognized by an object. User Actions

▪ Mouse Click▪ Entering Text▪ Pressing a Key

Program Calculations Triggered by the system

▪ Timer Event Handler: Code that is written by the

programmer to respond to an event Executes only when particular event occurs

Page 19: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Common Form Events Form1_Load() - Occurs before a form is

displayed for the first time. Form1_Activated() - Occurs when form

becomes the active window - through code or by user

Form1_Deactivate() - Occurs when the form loses focus and is not the active form

Form1_Closing() - Occurs when the form closes, either through an event or the windows close button being clicked

Page 20: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Many controls share a Common set of events to which they can react Click, DoubleClick MouseMove, MouseDown, MouseUp,

MouseWheel, MouseHover, MouseLeave KeyPress, KeyDown, KeyUp Resize DragDrop GotFocus LostFocus

Page 21: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Focus is when an object becomes the “Active Control”

Focus Event Sequence: Enter GotFocus Leave Validating Validated LostFocus

Page 22: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Create Event Procedure Double Click on Control Displays Code Window and Event Procedure

Stub for default eventOr

Open the Code Editor (F7 or View Menu:Code Command)

Select Control & Event from drop down windows in Code Editor

Event Code Goes In Here

Page 23: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Exit Button – Clicked Method (btnExit_Click)

Private Sub btnExit_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnExit.Click' End the applicationEnd

End Sub

Line Continuation Mark

Name of the event the procedure responds to

Name of the control that owns the event procedure

Marks the beginning of this event procedure

Ends the program

Event handled by this procedure

Page 24: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Input Controls

Process Events

Output Controls

Page 25: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

UDIE – Implement the solution in VB:Create the Interface

Input Controls Output Controls

Set the Properties Configure the appearance and behavior of the

controlsWrite the Code to execute when events

occur Process the inputs to create the outputs

Page 26: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Using Visual Basic.Net create the following form

ObjectObject PropertyProperty SettingSetting

Form1Form1 TextText DemonstrationDemonstration

txtFirsttxtFirst TextText (blank)(blank)

txtSecondtxtSecond TextText (blank)(blank)

btnRedbtnRed TextText Change Color Change Color to Redto Red

Page 27: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

When btnRed is clicked - Change txtFirst text color to red

Double Click on btnRedCode window should appear

(with Event Procedure Stub)

Add code to the event procedure stub: txtFirst.ForeColor = Color.Red

Page 28: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

When the text is edited in txtFirst - Change txtFirst text color to blue

In Code WindowSelect the Control for the Event Procedure

txtFirst from the ClassName boxSelect the Event from the Method Name Box

TextChanged

Class Name Box

Method Name Box

Page 29: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Add code to the event procedure stub: txtFirst.ForeColor = Color.Blue

Page 30: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

When txtFirst is deselected - Change txtFirst text color to black

In Code WindowSelect the Control for the Event Procedure

txtFirst from the ClassName boxSelect the Event from the Method Name Box

LeaveAdd code to the event procedure stub:

txtFirst.ForeColor = Color.Black

Page 31: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Click F5 or the Run ButtonType “Hello” into the 1st textbox

What HappensClick on the 2nd Textbox

What happened in txtFirst and Why Click on the Button

What happened in txtFirstType “Friends” into the 1st textboxStop Program by clicking Red X in

corner

Page 32: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Add a Button to your Form Name: btnExit Text Property: &Quit

Add a Button Click Event for this Button Code: END

Page 33: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Finds Syntax Errors (Errors in Programming Language)

Return to btnRed Click Event ProcedureAdd this line of Code:

txtSecond.text = Hello

Notice Wavy Blue Line – This indicates a Syntax Error that must be fixed.

Page 34: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Test All Events

Click Quit Button

Page 35: CIS 115 Lecture 3.  Forms  Form properties  Controls  Control properties  Event Driven Programming  Form Events  Control Events  Event Handlers.

Homework 1 Visual Basic Controls and Properties See handout for details and due date Questions?