multiple forms & procedures

29
Multiple Forms & Procedures

Upload: weylin

Post on 23-Feb-2016

41 views

Category:

Documents


0 download

DESCRIPTION

Multiple Forms & Procedures. Form. Methods: Show, Hide, Activate, Close Events: Load, Activated, Closing, Closed. Form Closing Event Example. If MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) = DialogResult.Yes Then e.Cancel = False Else e.Cancel = True - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Multiple Forms & Procedures

Multiple Forms & Procedures

Page 2: Multiple Forms & Procedures

Form

• Methods:– Show, Hide, Activate, Close

• Events:– Load, Activated, Closing, Closed

Page 3: Multiple Forms & Procedures

Form Closing Event Example

If MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) = DialogResult.Yes Then

e.Cancel = False

Else

e.Cancel = True

End If

Page 4: Multiple Forms & Procedures

Modeless or Modal

• Modeless form: Other forms can receive input focus while this form remains active.– formName.Show()

• Modal form: No other form can receive focus while this form remains active.– formName.ShowDialog()

Page 5: Multiple Forms & Procedures

Multiple FormsTwo forms: Form1, Form2To Open Form2 from Form1:

Dim f2 As New Form2() f2.Show()

Open Form2 as a Modal form:f2.ShowDialog()

Note: Form is defined as a class. Must create an instance of the form class by using the keyword New to access the form.

Page 6: Multiple Forms & Procedures

SharingVariables Among Forms

• Define these variables with class-level scope using the Public keyword.– Must use formName to qualify variables.– – Dim f2 As New Form2()– TextBox1.Text = f2.g2 *** g2 is declared in form2

Page 7: Multiple Forms & Procedures

SharingVariables Among Forms

• Define these variables with project-level scope in a module using the Public keyword:

– Module Module1– Public testVar As Integer– End Module

– Note: Use Project/Add Windows form to add a module.

Page 8: Multiple Forms & Procedures

Modules

• A file contains code such as:– Variable declarations– Procedures and functions

• Variables and procedures used by more than one form should store in a module.

• Global variables: Public

Page 9: Multiple Forms & Procedures

Starting Application with Sub Main

• The Sub Main procedure must reside in a module.

• Choose Sub Main as startup object. The code in Main will execute.– Write code in Main to open forms.

Page 10: Multiple Forms & Procedures

Sub Main ExamplePublic Sub main()

Dim dateToday As Date

dateToday = Today

If dateToday.DayOfWeek = DayOfWeek.Saturday Or dateToday.DayOfWeek = DayOfWeek.Sunday Then

Dim frmWeekEnd As New Form2()

frmWeekEnd.ShowDialog()

Else

Dim frmWeekDay As New Form1()

frmWeekDay.ShowDialog()

End If

End Sub

Page 11: Multiple Forms & Procedures

Declare Form’s Instance Variable in An Open Form Event Procedure

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim f2 As New Form2()

f2.Show()

End Sub

Problem: This will create a new instance of the form every time the button is clicked.

Page 12: Multiple Forms & Procedures

Declare Form’s Instance Variable in a Module as Public Variable

Module Module1 Public f1 As New Form1() Public f2 As New Form2()End ModuleIt can be accessed in a procedure:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click f2.Show() End Sub

Problem: Once the form is closed the variable is no longer exists. Attempt to access it again will trigger error.

Page 13: Multiple Forms & Procedures

Close Form Or Hide Form

Use a form’s Closing event to change Close to Hide:

Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

e.Cancel() = True

Me.Hide()

End Sub

Page 14: Multiple Forms & Procedures

Main Menu Control

– Add MainMenu control and follow the TypeHere instruction.

– Each submenu and each item on a submenu is represented by a MenuItem control.

– Use an & to specify an access key in the caption. Ex. &File, Sho&s

– Write an event procedure for each menu item.

Page 15: Multiple Forms & Procedures

• Shortcut keys– Select the Shortcut key property in the MenuItem’s

property window.– Select the Shorcut key from list.– Set ShowShortcut property to true.

• Separator bar– Right clock a menu item/Insert Separator

• Inserting, deleting a menu item– Right click and select the option.

• Rearranging menu items– dragging

Page 16: Multiple Forms & Procedures

Context Menu

• A context menu is a menu that displays when an object on the screen is right-clicked.

• Add the ContextMenu control (it is placed in a tray under the form). Right-click the control and choose Edit to create the menu.

• Use the object’s ContextMenu property to bind the object to the context menu.

Page 17: Multiple Forms & Procedures

Simple Text Editor

• Textbox’s properties, methods– Help– Object Browser

• System.Windows.Forms

• Menu: Undo, Copy, Cut, Paste, SelectAll, Search • Clipboard:

– Clipboard.SetDataObject(TextBox1.SelectedText)– Clipboard.GetDataObject()

Page 18: Multiple Forms & Procedures

Procedures

. Sub procedure:Private/Public Sub SubName(Arguments)

…End Sub

Private: Can only be accessed by procedures in the same form.

Public: Can be accessed by procedures in other forms.

• To call a sub procedure SUB1• Call SUB1(Argument1, Argument2, …)

Page 19: Multiple Forms & Procedures

Function

• Private Function tax(salary) As Double• tax = salary * 0.1• End Function

– Or• Private Function tax(salary)• Return salary * 0.1• End Function

Page 20: Multiple Forms & Procedures

Call by Reference Call by Value

• ByRef– Default– The address of the item is passed. Any changes

made to the passing variable are made to the variable itself.

• ByVal– Only the variable’s value is passed.

Page 21: Multiple Forms & Procedures

ByRef, ByVal examplePrivate Sub Command1_Click()

Dim myStr As String

myStr = TextBox1.Text

ChangeTextRef (myStr)

TextBox1.Text = myStr

End Sub

Private Sub ChangeTextRef(ByRef strInput As String)

strInput = "New Text"

End Sub

Page 22: Multiple Forms & Procedures

Input, Output Arguments

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sal, tax As Single sal = CSng(TextBox1.Text) Call CalTax(sal, tax) TextBox2.Text = tax.ToString End Sub Private Sub CalTax(ByVal Salary As Single, ByRef Tax As Single) Tax = 0.1 * Salary End Sub

Can we pass the Tax ByVal?

Page 23: Multiple Forms & Procedures

Static Variables

• Static VariableName as DataType• Static variables are local variables but are

not destroyed when a procedure terminates. Their value are kept.

Page 24: Multiple Forms & Procedures

Static Variable Example

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Static clickCounter As Integer = 0

clickCounter += 1

If clickCounter > 3 Then

MsgBox("Sorry, you can only click 3 times!")

Button1.Enabled = False

End If

End Sub

Page 25: Multiple Forms & Procedures

Event Procedures

• Example:– Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click

• The Handles keyword– Procedure name may change

• Handling multiple events:– Private Sub AllButtons_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click, Button2.CLick

Page 26: Multiple Forms & Procedures

Using One Event Handler to Handle Events Generated by Many Controls• Assume we have 3 buttons.• Use the Handles clause in the event

procedure to associate controls with the event procedure.

• We can assign a value for each control’s Tag property, or use control’s TabIndex property to determine which control generates the event.

Page 27: Multiple Forms & Procedures

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

If sender.tag = "1" Then

MessageBox.Show("button 1 clicked")

ElseIf sender.tag = "2" Then

MessageBox.Show("button 2 clicked")

Else

MessageBox.Show("button 3 clicked")

End If

End Sub

Note: VB IntelliSense will not show the Tag property after you type sender.

Page 28: Multiple Forms & Procedures

Early Binding/Late Binding

• Early binding: VB compiler knows the object’s data type. It enables the use of IntelliSense.

• Late binding: VB compiler can’t determine the type of object that we are calling. This occurs because the object is declared as Object data type.

Page 29: Multiple Forms & Procedures

Demo: Phone Simulator