Download presentation
Presentation is loading. Please wait.
1
VB.Net Loops
2
Loop FOR index – start TO end [STEP step] [statements] [EXIT FOR] NEXT index DO [{WHILE| UNTIL} condition] [statements] [EXIT DO] LOOP
3
Do While/Do Until Private Sub Command1_Click() Dim counter As Integer counter = 0 Do While counter <= 5 Debug.Write(counter) counter = counter + 1 Loop Text1.Text = counter End Sub Private Sub Button2_Click() Dim counter As Integer counter = 0 Do Until counter > 5 Debug.Write(counter) counter = counter + 1 Loop Text1.Text = counter End Sub
4
Loop Demo:Future Value Dim output As String = "Year" & vbTab & "FutureValue" & vbCrLf Dim output2 As String = "Year" & vbTab & "FutureValue" & vbCrLf Dim amount, principal, iRate, year As Single Dim yearIndex As Integer principal = CSng(TextBox1.Text) iRate = CSng(TextBox2.Text) year = CSng(TextBox3.Text) For yearIndex = 1 To CInt(year) amount = principal * (1 + iRate) ^ yearIndex output = output & yearIndex & vbTab & FormatCurrency(amount) & vbCrLf output2 = output2 & yearIndex & vbTab & String.Format("{0:c}", amount) & vbCrLf Next MessageBox.Show(output, "FutureValue", MessageBoxButtons.OK) MessageBox.Show(output2, "FutureValue", MessageBoxButtons.OK) Note: Use the Do While, Do Until loops.
5
Formatting Numbers for Output FormatNumber, FormatCurrency, FormatPercent –(NumericExpression [,Decimal points]. …), FormatDateTime(DateExpression, date format) String.Format(formatSring, Arg0, Arg1,…) –Ex: str=String.Format(“{0,10} {1,20}”, x, y) “0” represents the first number, “10” reserves 10 spaces. –Str=String.Format(“The 1 st number is {0} the 2 nd number is {1}”, x, y) –Str=String.Format(“{0,10:C}”, amount) –Format code: C, N, P, E, etc. Special characters: –vbTab, vbCrLf
6
Example: Display characters entered in textbox1one at a time Dim index As Integer index = 0 For index = 0 To TextBox1.TextLength - 1 MessageBox.Show(TextBox1.Text.Substring(index, 1)) Next Problem 1: How many “a” entered in textbox1? Problem 2: How many words entered in textbox1?
7
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 with code. –SelectionMode: one or multi selection –SelectedItem(s), SelectedIndex Methods –Add –Clear
8
List Box Example Private 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
9
Selected Item’s Value Demo: –Select interest rate from a list box.
10
ListBox Multiple Selections Note: The SelectionMode property must set to multi selection. extBox2.Text = ListBox1.SelectedItems.Count.ToString Dim allSel As String Dim i As Integer For i = 0 To ListBox1.SelectedItems.Count - 1 allSel = allSel & ListBox1.SelectedItems.Item(I) Or allSel = allSel & ListBox1.SelectedItems(I) Next ‘**** Or use the For Each loop**** Dim item As String For Each item In ListBox1.SelectedItems allSel = allSel & item Next TextBox3.Text = allSel
11
Collections Collections are used to store lists of objects. A collection’s name usually end with a “s”. –Items –SelectedItems
12
List Items Collections Methods: –ADD: ListBox1.Items.Add("Apple") –Item: Retrieve an object from Items ListBox1.Items.Item(Index) 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) –Listbox1.Refresh() –Clear: ListBox.Items.Clear() –Count: Return the number of objects in a collection.
13
Iterating Through a Collection Dim msg As String = "you select:" Dim i As Integer = 0 For i = 0 To ListBox1.SelectedItems.Count - 1 msg = msg & ListBox1.SelectedItems(i) Next TextBox1.Text = msg Dim Msg2 As String = "You select: " Dim sitem As String For Each sitem In ListBox1.SelectedItems Msg2 = Msg2 & sitem Next TextBox2.Text = Msg2
14
Using ListBox to Display Output ListBox1.Items.Clear() ListBox1.Items.Add("Year" & vbTab & "FutureValue") For yearIndex = 1 To CInt(year) amount = principal * (1 + iRate) ^ yearIndex ListBox1.Items.Add(yearIndex.ToString & vbTab & FormatCurrency(amount)) Next
15
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?
16
Structured Error Handling Try 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
17
Enumerations Provide a way to associate meaningful names with a sequence of constant values. Define an enumeration using an Enum statement in classes, modules, and structures. –Private Enum seasonOfYear –Spring = 1 –Summer = 2 –Fall= 3 –Winter = 4 –End Enum
18
Enumerations Enum enumTest spring = 1 summer fall winter End Enum Dim etest As enumTest Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load etest = enumTest.summer TextBox1.Text = Val(etest).ToString Provide a way to associate meaningful names with a sequence of constant values.
19
Dim i, j, wordCount As Integer Dim trimedText As String trimedText = TextBox1.Text.Trim() wordCount = 0 If trimedText.Length = 0 Then wordCount = 0 Else For i = 0 To trimedText.Length - 1 If trimedText.Substring(i, 1) = Chr(Keys.Space) Then wordCount += 1 For j = i To trimedText.Length - 1 If Not trimedText.Substring(j, 1) = " " Then i = j Exit For End If Next End If Next wordCount += 1 End If MessageBox.Show(wordCount.ToString & "words")
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.