Upload
virginia-henry
View
223
Download
1
Tags:
Embed Size (px)
Citation preview
Looping in VB
What are the different types of loops?◦ Do….While
Performs statements within loop while a condition is true◦ Do….Until
Performs statements within loop until a condition is true◦ For….Next
Performs statements within the loop a pre-determined number of times
Within these there are two styles◦ Post-test
Code is executed at least once◦ Pre-test
Code may not get execute.
Just to Review
Can be top controlled or bottom controlled Top Controlled syntax
Do While condition Body of loopLoop
Bottom Controlled syntaxDoBody of loopLoop while condition
Looping in VB ~ Do…While
Top-Controlled Do While Loops
Bottom-Controlled Do While Loop
Much like the Do…While can be top or bottom controlled
Top ControlledDo until conditionBody of loopLoop
Bottom ControlledDoBody of loopLoop until condition
Loop in VB ~ Do….Until
Repeating a Process Using the For…Next Loop You can use a For...Next loop when a section
of code is to be executed an exact number of times
Repeating a Process Using the For…Next Loop
Priming the Loop Starting a loop with a preset value in the
variable(s) tested in the condition is called priming the loop
10
Using a DataTip with Breakpoints Resolving defects in code is called
debugging Breakpoints allow us to stop the code where
ever we want so that we can examine what is going on with our variables.
While in break mode, you can examine the values in all
Chapter 6: Loop Structures 11
Accumulators & Counters
A variable that contains an accumulated value such as the total of all the speeds is called an accumulator
A variable that always is incremented by a constant value is called a counter◦ How many vehicle speeds the user has entered
Chapter 6: Loop Structures 12
Compound Operators
A compound operator allows you to add, subtract, multiply, divide, use modulus or exponents, or concatenate strings, storing the result in the same variable
Chapter 6: Loop Structures 13
Examples
Chapter 6: Loop Structures 14
Examples
15
MenuStrip Object A menu bar is a strip across the top of a
window that contains one or more menu names
A menu is a group of commands, or items, presented in a list
Chapter 6: Loop Structures 16
MenuStrip Object
Chapter 6: Loop Structures 17
Event Handlers for Menu Items Work the same for buttons – double click on
the menu item and the code window should pop up
18
Standard Items for a Menu Action Tag allows you to create a full standard
menu bar commonly provided in Windows programs
Can specify a set of actions, called smart actions, for an object as you design a form
To do this place a new MenuStrip object on your form
Click the Action Tag on the MenuStrip object Click Insert Standard Items on the MenuStrip Tasks
menu Click File on the menu bar to view the individual
menu items and their associated icons on the File menu
19
Standard Items for a Menu
20
InputBox Function The InputBox function displays a dialog box,
that asks the user for some type of input. The User can either click on OK or Cancel When the user enters the text the InputBox
function returns this text as a string If the user clicks the Cancel button, the
function returns a null string ("")
Chapter 6: Loop Structures 21
InputBox Object Default Value The InputBox object can be assigned a
default value
22
Example
23
List Boxes List boxes can be used to display items for a
user to pick from OR receive output from a program
24
Adding Items During Design Click the Items property in the Properties window Click the ellipsis button in the right column of the
Items property Click in the String Collection Editor window. Type
in whatever values you want and click on the OK button
Selected Item Property When we use a list box to display a set of
values for the user to pick from, when they pick their item, we need to bring it in to the code
ListBox Items.Count Property This property returns an integer with the
number of entries stored in the Items property
Example of use:
The number of entries in the list can be assigned to an integer variable
If lstEmployees.Items.Count = 0 ThenMessageBox.Show("The list has no items!")
End If
numEmployees = lstEmployees.Items.Count
Item Indexing The Items property values can be accessed
from your VB code Each item value is given a sequential index
◦ The first item has an index of 0◦ The second item has an index of 1, etc.
Example:
name = lstCustomers.Items(2)' Access the 3rd item value
ListBox SelectIndex Property
The SelectIndex property returns an integer with the index of the item selected by the user
If no item is selected, the value is set to -1 (an invalid index value)
Can use SelectIndex to determine if an item has been selected by comparing to -1
Example:If lstLocations.SelectedIndex <> -1 Then
location = lstLocations.Items(lstLocations.SelectedIndex)End If
ListBox SelectedItem Property Instead of using the SelectedIndex property
as follows:
The SelectedItem property can be used to retrieve the value of a selected item as follows:
If lstMonths.SelectedIndex <> -1 Thenmonth = lstMonths.Items(lstMonths.SelectedIndex)
End If
If lstMonths.SelectedIndex <> -1 Thenmonth = lstMonths.SelectedItem.ToString)
End If
ListBox Sorted Property Sorted is a boolean property When set to true, values in the Items
property are displayed in alphabetical order When set to false, values in the Items
property are displayed in the order they were added
ListBox Items.Add Method Items can be added to the end of a ListBox
list in your VB code using the Add method Format is ListBox.Items.Add(Item)
ListBox is the name of the control Item is a string value to add to the Items
property Example:
lstStudents.Items.Add("Sharon")
ListBox Items.Insert Method Items can be added at a specific position of
a ListBox in VB code using the Insert methodListBox.Items.Insert(Index, Item)
Index specifies position where Item is placed
Index is zero based similar to SelectedIndex property
Items that follow are “pushed” down Example inserting "Jean“ as the 3rd itemlstStudents.Items.Insert(2, "Jean")
ListBox Methods to Remove Items ListBox.Items.RemoveAt(Index)
◦ Removes item at the specified index ListBox.Items.Remove(Item)
◦ Removes item with value specified by Item ListBox.Items.Clear()
◦ Removes all items in the Items property Examples:lstStudents.Items.RemoveAt(2) ‘remove 3rd itemlstStudents.Items.Remove(“Jean”) ‘remove item JeanlstStudents.Items.Clear() ‘remove all items
34
Using a DataTip with Breakpoints Right-click where ever you wish to put your
breakpoint, point to breakpoint on the short cut menu.
To run and test the program with the breakpoint, click the Start Debugging button on the Standard toolbar
To see the value of the variable hover your mouse over the variable declaration and you will see the current value.
Using a DataTip with Breakpoints
36
Using a DataTip with Breakpoints To remove a breakpoint, right-click the
statement containing the breakpoint, and then point to Breakpoint on the shortcut menu
Click Delete Breakpoint on the Breakpoint submenu
37
Creating the Executable
After you complete your application, how do we create the executable or deploy it?
Deploying a project means placing an executable version of the program on your hard disk, on a Web server, or on a network server
You can create a deployed program by using ClickOnce Deployment
The deployed version of the program you create can be installed and executed on any computer that has the .NET framework installed
38
Publishing an Application
With the program open, click Build on the menu bar
Click Publish Radar on the Build menu Change the default location from publish\ to a
file location. Click the Next button. If necessary, click the
From a CD-ROM or DVDROM radio button Click the Next button. If necessary, click the
The application will not check for updates radio button
Click on next and then finish
Chapter 6: Loop Structures 39
Installing Your Software
To install the application, double-click the setup file
After installation, the program will run. To run the installed application again, click the Start button on the Windows taskbar. Point to All Programs, click Radar on the All Programs menu, and then click Radar on the Radar submenu
40
Creating the Executable