vb procedures. procedures. sub procedure: private/public sub subname(arguments) … end sub private:...

21
VB Procedures

Upload: abraham-haynes

Post on 01-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

VB Procedures

Page 2: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 3: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 4: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

Creating a IsAlphaNumeric(string) Function• IsNumeric: test a string

• IsDigit, IsLetter, IsLetterOrDigit: test a char– Char.IsLetterOrDigit(char)

Page 5: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

Function IsAlphaNumeric(ByVal aString As String) As Boolean Dim i As Integer = 0 Dim AlphaNumeric As Boolean = True For i = 0 To aString.Length - 1 If Not Char.IsLetterOrDigit(aString.Substring(i, 1)) Then AlphaNumeric = False Exit For End If Next If AlphaNumeric = True Then IsAlphaNumeric = True Else IsAlphaNumeric = False End If End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If IsAlphaNumeric(TextBox1.Text) Then MessageBox.Show("textbox1 is alphanumeric") Else MessageBox.Show("textbox1 is not alphanumeric") End If End Sub

Page 6: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 7: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

ByRef, ByVal example

Private 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 8: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 9: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

Static Variables

• Static VariableName as DataType

• Static variables are local variables but are not destroyed when a procedure terminates. Their value are kept.

Page 10: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 11: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 12: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 13: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 14: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 15: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

Demo: Phone Simulator

Page 16: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

Menu

Page 17: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 18: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

• 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 19: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

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 20: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

Simple Text Editor

• Textbox’s properties, methods– Help

– Object Browser• System.Windows.Forms

– Cut, Copy, Paste, Undo, SelectAll, etc.

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

– Clipboard.SetDataObject(TextBox1.SelectedText)

– Clipboard.GetDataObject()

Page 21: VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form

FontDialog Control

• If FontDialog1.ShowDialog() = DialogResult.OK Then

• TextBox1.Font = FontDialog1.Font

• End If