VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard...
-
date post
19-Dec-2015 -
Category
Documents
-
view
228 -
download
0
Transcript of VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard...
![Page 1: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/1.jpg)
VB.NET User Interface Controls
![Page 2: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/2.jpg)
VB User Interface Objects• Form• InputBox, MessageBox• Standard Controls:
– TextBox, MaskedTextBox, List Box, Option Button, Check Box, CheckedListBox, Command Button, GroupBox, etc.
• Menu:– MenuStrip, ContextMenuStrip– ToolStrip
• ToolStripContainer– ToolTip
• Others:– Containers– Components– Dialogs
![Page 3: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/3.jpg)
Form• Form is defined as a class. • Methods:
– Show, ShowDialog: Open a form
– Activate, Focus: Make an opened form get focus
– Hide, Close
– Ex.• Me.Hide, Me.Close
Note: Closing a form is equivalent to delete a form.• Events:
– Load, Activated, DeActivate, Closing, Closed
![Page 4: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/4.jpg)
Multiple FormsTwo forms: Form1, Form2To Open Form2 from Form1:
Standard but troublesome way to open a form: Must create an instance of the form class by using the keyword New to access the form.
Dim f2 As New Form2()
f2.Show()
Open Form2 as a Modal form:
f2.ShowDialog()
.
![Page 5: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/5.jpg)
• 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()
• Demo: Problem with the Show method
![Page 6: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/6.jpg)
Using the Default Instances of Forms to Open a Form
• formName.Show, formName.ShowDialog
• Always bring up the same underlying default instance of the form.– Example: Form2.ShowDialog
![Page 7: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/7.jpg)
MessageBoxMessageBox.Show(message)
MessageBox.Show(message, Caption)
MessageBox.Show(message, Caption, Buttons)
Note:
1. In each format, arguments are positional and required.
2. This object returns a DialogResult data type. Possible values for a DialogResult data type are: Abort, Cancel, Ignore, No, None, OK, ReTry, and Yes. To test the return value:
Dim ReturnVal as DialogResult
ReturnVal=MessageBox(“hello”, …..)
If ReturnVal=DialogResult.OK…
![Page 8: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/8.jpg)
Form Closing Event Example
Private Sub Form10_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) = DialogResult.Yes Then e.Cancel = False Else e.Cancel = True End If End Sub
Note: Event procedure arguments: sender: object that triggers the event.e: event object
![Page 9: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/9.jpg)
InputBoxInputBox(Prompt [,Title] [, Default] [, Xpos] [, Ypos])
Xpos is the distance from the left edge of the screen, and Ypos is the distance from the top of the screen. Both are measured in twips (1/1440 th of an inch).
Note: The arguments are positional and optional. Enter a comma to skip an argument.
cityName = InputBox("Please enter city name:“, , “SF”)
If cityName = vbNullString Then
MessageBox.Show ("customer click cancel")
Else
Text1.Text = cityName
End If
Note: vbNullString is a VB constant representing null value. All VB constants are listed in Constants collection.
![Page 10: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/10.jpg)
Text Box
• Useful properties– BackColor, BorderStyle– ReadOnly– Enable– Visible– Password Character– Multiline– ScrollBar– Text
• Useful events– TextChanged: default event– Validating
![Page 11: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/11.jpg)
Input Validation• Numbers are checked to ensure they are:
– Within a range of possible values– Reasonableness– Not causing problems such as division by 0.– Containing only digits
• IsNumeric
• Texts are checked to ensure correct format.– Phone #, SSN.
• Required field• Textbox:
– Set CauseValidation property to true.– Use the Validating event:
• Triggered just before the focus shifts to other control.
![Page 12: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/12.jpg)
TextBox Validating Event IsNumeric function
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If Not IsNumeric(TextBox1.Text) Then
e.Cancel = True
MessageBox.Show("enter digits only")
Else
MessageBox.Show("good")
End If
End Sub
Note: Why not use the TextChanged event?
![Page 13: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/13.jpg)
MaskedTextBox
• To select a mask:– Click the smartTag and choose Set Mask, or– Use the mask property to choose a mask.
• Custom mask:– Search Help– Example:
• (???)-###
![Page 14: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/14.jpg)
String Methods
• ToUpper, ToLower• Length – Number of characters• TrimStart, TrimEnd, Trim• Substring(Start), Substring(Start, length)• IndexOf(SearchString), IndexOf(SearchString, Start)
– 0 based index– Case-sensitive
• eName=“David”• Position=eName.IndexOf(“d”)
– Return –1 if the searchString is not found.
• Note: Text property of a Textbox has all the string methods.– Ex. TextBox1.Text.Substring(0,2)
![Page 15: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/15.jpg)
Example: Extract the firstname and the lastname from a fullname • Dim indexSpace As Integer• Dim firstName, lastName As String• indexSpace = TextBox1.Text.IndexOf(" ")• firstName = TextBox1.Text.Substring(0, indexSpace)
• lastName = TextBox1.Text.Substring(indexSpace + 1)
• MessageBox.Show(firstName)• MessageBox.Show(lastName)
![Page 16: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/16.jpg)
Validate SSN Format (Or Use MaskedTextbox)Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim correct As Boolean = True
If Not IsNumeric(TextBox1.Text.Substring(0, 3)) Or _
Not IsNumeric(TextBox1.Text.Substring(4, 2)) Or _
Not IsNumeric(TextBox1.Text.Substring(7, 4)) Then
correct = False
End If
If TextBox1.Text.Substring(3, 1) <> "-" Or TextBox1.Text.Substring(6, 1) <> "-" Then
correct = False
End If
If correct Then
MessageBox.Show("perfect format")
Else
e.Cancel = True
MessageBox.Show("not correct format")
End If
End Sub
![Page 17: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/17.jpg)
Group Box
• It is a container control.
• Controls in a Group Box should move with the box.
![Page 18: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/18.jpg)
Radio Button
• Radio buttons must be grouped together inside a container such as a GroupBox or a form.
• When the user selects an option all other options in the same group are deselected.
• Properties:– Checked: True/False.
• Default button: Set the Checked property to true at the design time.
• Events: – CheckedChanged
![Page 19: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/19.jpg)
RadioButton Example 1
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
MessageBox.Show("Check RadioButton1")
Else
MessageBox.Show("uncheck RadioButton1")
End If
End Sub
![Page 20: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/20.jpg)
RadioButton Example 2
If radioButton1.Checked=true then
textbox1.text=“You select radio button 1”
ElseIf radioButton2.Checked=true then
textbox1.text=“You select radio button 2”
Else
textbox1.text=“You select radio button 3”
End If
![Page 21: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/21.jpg)
Check Box
• Check boxes do not belong to a group even when they are grouped in a Group Box.
• Checked property and checkedChanged event
![Page 22: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/22.jpg)
Check Box Example 1Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
MessageBox.Show(“check chk1")
Else
MessageBox.Show("uncheck chk1")
End If
End Sub
![Page 23: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/23.jpg)
Check Box Example 2Dim msg as String
Msg=“You choose “
If checkBox1.checked=true then
msg=msg & “check box 1”
End If
If checkBox2.checked=true then
msg=msg & “check box 2”
End If
If checkBox3.checked=true then
msg=msg & “check box 3”
End If
Note: Cannot put these three conditions in a If …ElseIf block.
![Page 24: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/24.jpg)
List Box
• Useful properties– Items: The items in the listBox. It is a collection
strcture. Items can be entered at the design time or entered in code.
• 0-based index– SelectionMode: one or multi selection– SelectedItem(s)– MultiColumn
• Methods– Add– Clear
• Event: SelectedIndexChange
![Page 25: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/25.jpg)
List Box ExamplePrivate Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Clear()
TextBox2.Clear()
ListBox1.Items.Clear()
ListBox1.Items.Add("Apple")
ListBox1.Items.Add("Orange")
ListBox1.Items.Add("Banana")
ListBox1.Items.Add("Strawberry")
TextBox2.Text = ListBox1.Items.Count.ToString
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedItem
End Sub
![Page 26: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/26.jpg)
List Items Collections• Methods:
– ADD: ListBox1.Items.Add("Apple")
– Item: Retrieve an object from Items• ListBox1.Items.Item(Index) or ListBox1.Items(Index)
• 0-based index
– Insert: ListBox.Items.Insert(Index, item)
– Remove: Delete an object with a position index or key.• ListBox.Items.Remove(Item)
• ListBox.Items.RemoveAt(Index)
– Clear: ListBox.Items.Clear()
– Count: Return the number of objects in a collection.• ListBox.Items.Count
![Page 27: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/27.jpg)
Selected Item’s Value
• Demo:– Select interest rate from a list box:
• 5% -> 0.05
– Dim intRate As Double– Select Case ListBox1.SelectedItem– Case "5% "– intRate = 0.05– Case “6%”– intRate = 0.06– Case “7%”– intRate = 0.07– End Select
![Page 28: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/28.jpg)
CheckedListBox
• ItemCheck event– Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object,
ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
– TextBox2.Text = CheckedListBox1.SelectedItem– End Sub
• Multiple selections:– CheckedItems– CheckedIndices– GetItemChecked
![Page 29: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/29.jpg)
Using CheckedIndices and GetItemChecked Properties
Dim i As Integer For Each i In CheckedListBox1.CheckedIndices MessageBox.Show(CheckedListBox1.Items(i)) Next For i = 0 To CheckedListBox1.Items.Count - 1 If CheckedListBox1.GetItemChecked(i) = True Then MessageBox.Show(CheckedListBox1.Items(i)) End If Next
![Page 30: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/30.jpg)
ComboBox• Allows the user to type text directly into the
combo box.• Use the Text property to get entered item:
– ComboBox1.Text– The index for an entered item is –1.– SelectedItem may be different from the Text property.
• Search an item in the list: ComboBox1.Items.IndexOf(“search text”)– Found: return the index of the search text.– Not found: return –1.
• How to add an entered item to the list?
![Page 31: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/31.jpg)
Timer
• Timer– Properties:
• Enabled -- must set to True.• Interval
– Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
label1.Text = System.DateTime.Now.ToString End Sub
![Page 32: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/32.jpg)
Use a Timer to Close a Form
Dim counter As Integer = 0Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick counter = counter + 1 If counter > 100 Then Me.Close() End If End Sub
![Page 33: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/33.jpg)
Structured Error HandlingTry
result = Val(TextBox1.Text) / Val(TextBox2.Text)
TextBox3.Text = result.ToString
Catch except As DivideByZeroException
MessageBox.Show(except.Message)
Catch except As Exception
MessageBox.Show(except.Message)
Finally
MessageBox.Show("I get exdecuted, no matter what")
End Try
![Page 34: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/34.jpg)
MenuStrip Control
– Add MenuStrip control and follow the TypeHere instruction.
– Each submenu and each item on a submenu is represented by a MenuItem control.
– Write an event procedure for each menu item.
![Page 35: VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,](https://reader035.fdocuments.us/reader035/viewer/2022062221/56649d395503460f94a12bad/html5/thumbnails/35.jpg)
Using One Event Procedure to Handle Many Events
Private Sub BtnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click If sender.name = "Button1" Then MessageBox.Show("btn 1") ElseIf sender.name = "Button2" Then MessageBox.Show("Btn2") Else MessageBox.Show("btn3") End If End Sub
Note 1: Controls’ Tag propertyNote 2: Late binding