VB.Net Loops.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Practical Programming COMP153-08S Lecture: Repetition Continued.
Creating Custom Controls ISYS 512/812. Inheritance The process in which a new class can be based on an existing class, and will inherit that class’s interface.
VB.NET User Interface Controls
VB Classes - 2 ISYS 573. Creating an Array of Objects Dim emps(2) As emp Dim i As Integer For i = 0 To emps.GetUpperBound(0) emps(i) = New emp() Next.
Programming Based on Events
Control structures Part 2 iteration control To enable repetition of a statement block.
VB.NET Database Tools ISYS Net Applications OLE DB Provider OLE DB Data Source OLE DB Provider ODBC Data Source SQL Server Data Source SQL Server.Net.
VB Collection Data Structures. Array ArrayList HashTable VB6 Collection Others:SortedList, Stack, Queue.
Coding ADO.NET Objects: Connection, Command, DataReader.
Programming Interface Controls with VB.Net. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
VB.NET Database Tools ISYS 573. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Arrays. Declaring a Array With subscript: –Dim numbers(2) as Integer –Using variable as subscript: Dim arrayIndex as Integer = 10 Dim myArray(arrayIndex)
Coding ADO.NET Objects: Connection, Command, DataReader.
More on lists, exceptions, loops and validation. You can use the exception to indicate the error that occurred Private Sub btnCheck_Click(ByVal sender.
List-based Controls. Slide 2 Introduction There are several controls that work with lists ComboBox ListBox CheckedListBox.
VB.Net Loops. Loop FOR index – start TO end [STEP step] [statements] [EXIT FOR] NEXT index DO [{WHILE| UNTIL} condition] [statements] [EXIT DO] LOOP.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Programming Interface Controls with VB.Net. User Interface Controls Form MessageBox, InputBox Common Controls: –Button, TextBox, MaskedTextBox, List Box,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Tutorial 6 The Repetition Structure
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
1.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
 A ListBox control displays a list of items and allows the user to select one or more  Drag from Toolbox to create this control on a form.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Unit 6 Repetition Processing Instructor: Brent Presley.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Directory and File. Access Files in a Directory Name space: System.IO The Directory and File classes contain only shared methods that set or return information.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Single Dimensional Arrays
Apply Procedures to Develop Menus, List Box and Combo Box Objects
IS 350 Loops.
Chapter 5 – Control Structures: Part 2
Programming Interface Controls
Programming Interface Controls
Visual Basic..
Department Array in Visual Basic
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Lecture Set 10 Windows Controls and Forms
Coding ADO.NET Objects: Connection, Command, DataReader
Programming Interface Controls
Presentation transcript:

VB.Net Loops

Loop FOR index – start TO end [STEP step] [statements] [EXIT FOR] NEXT index DO [{WHILE| UNTIL} condition] [EXIT DO] LOOP

Do While/Do Until Private Sub Command1_Click() Dim counter As Integer Do While counter <= 5 Debug.Write(counter) counter = counter + 1 Loop Text1.Text = counter End Sub Private Sub Button2_Click() Do Until counter > 5

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. Note: vbCrLf is a VB keyword for a new line.

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?

Dim i, j, wordCount As Integer Dim trimedText As String trimedText = TextBox1.Text.Trim() wordCount = 0 If trimedText.Length = 0 Then 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 MessageBox.Show(wordCount.ToString & "words")

Dim i As Integer = 0 Dim j As Integer Dim wordCount As Integer = 0 Dim searchText As String searchText = TextBox1.Text If searchText.Length > 0 Then Do While True j = searchText.IndexOf(" ", i) If j < 0 Then wordCount += 1 Exit Do End If If j = i Then i += 1 Else i = j + 1 If i >= searchText.Length Then Loop MessageBox.Show(wordCount.ToString & "words")

List Box Useful properties Methods Events: SelectedIndexChanged event 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 Events: SelectedIndexChanged event

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

Selected Item’s Value Demo: Select interest rate from a list box.

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 TextBox3.Text = allSel

ListBox 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.

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

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?

Unstructured Error Handling On Error GoTo errorhandler (a label) Built-in Err object properties: Number – Error number Source – Name of VB ile in which error occurred Description – error message Continue the program execution: Resume: returns execution to the statement that caused the error. Resume Next Resume label: Jumps to the line containing the label. Exit Sub

Error Handling Example Private Sub cmdDivide_Click() On Error GoTo DivideErrorHandler lalAnswer.Caption=CStr(CDbl(Text1.text)/CDbl(Text2.Text)) Exit Sub DivideErrorHandler: MsgBox(CStr(Err.Number) & “: “ & Err.Description) End Sub

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 Finally MessageBox.Show("I get exdecuted, no matter what") End Try

Collections Collections are used to store lists of objects. More flexible than array: No need to declare the number of objects in a collection. Objects can be added, deleted at any position. Object can be retrieved from a collection by a key. Can store different types of objects. A collection’s name usually end with a “s”.

Using Collections Define a collection: Methods: Ex. Dim Pets as New Collection Methods: ADD: Add object to a collection Pets.Add(“dog”) Add an object with a key: Pets.Add(“Dog”, “D”) Item: Retrieve an object from a collection with a position index (base 1) or with a key. petName = Pets.Item(1) petName = Pets.Item(“D”) Count: Return the number of objects in a collection. Remove: Delete an object with a position index or key.

Iterating Through a Collection Dim Pets as New Collection … Dim Indx as Long For Indx = 1 to Pets.Count …operations … Next Indx For Each pet in Pets … operations … Next pet

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

Enumerations Provide a way to associate meaningful names with a sequence of constant values. 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