list boxes and combo boxes provides a list of items to select from various styles — choose based...

Download 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

If you can't read please download the document

Upload: osborn-hodges

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

Referencing the Items Collection Use the index of the item to reference a specific item in the collection. The index is zero based, so the first item in the list is index position zero. The index number of currently selected item is stored in the SelectedIndex property. SelectedIndex = -1 if no item in the list is currently selected. The property Count is the number of items in the list. SchoolsListBox.Items(1) = “Stanford"

TRANSCRIPT

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 Need to add to a list Filling a List at Design Time List of items in a ListBox or ComboBox is a collection. VB collections are objects that have properties and methods The Items property lets you add strings to the collection. Referencing the Items Collection Use the index of the item to reference a specific item in the collection. The index is zero based, so the first item in the list is index position zero. The index number of currently selected item is stored in the SelectedIndex property. SelectedIndex = -1 if no item in the list is currently selected. The property Count is the number of items in the list. SchoolsListBox.Items(1) = Stanford" Adding Items at Run Time The method add() is used to add new items to the list at run time. General Form: Examples: Object.Items.Add(ItemValue) SchoolsListBox.Items.Add("Harvard") MajorsComboBox.Items.Add(MajorString) Use the method insert() to add new items to the list at run time in a specific location (index position) in the collection. General Form: Examples: Object.Items.Insert(IndexPosition, ItemValue) SchoolsListBox.Items.Insert(0, "Harvard") MajorsComboBox.Items.Insert(1, MajorsString) Removing Items at Run Time The Items method Remove() removes an item by specifying the text of the item to be deleted. General Form: Example: The Items method RemoveAt() removes an item by index from the list. General Form: Example: SchoolsComboBox.Items.Remove(Brookdale) Object.Items.Remove(TextString) Object.Items.RemoveAt(IndexPosition) NamesListBox.Items.RemoveAt(0) Useful List Box and Combo Box Events TextChanged Event (ComboBox only) User types text into combo box Enter Event User tabs to the control and control gets focus. Leave Event User tabs out of the control and control loses focus. To add code to these events, double-click the event name in the Properties window after clicking the Events button. The editor will create the procedure header for you. Looping Statements A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Looping statements terminate based on a specified condition: Execution of the loop continues while a condition is True or until a condition is True. The loop executes an unpredictable and variable number of times. Or execution of the loop continues a predictable and fixed number of times. The Do/Loop Statement in Visual Basic Do While condition 'Looping statements Loop Do Until condition 'Looping statements Loop Pre-test Loops: test at top of loop Post-test Loops: test at bottom of loop Do 'Looping statements Loop While condition Do 'Looping statements Loop Until condition Do/Loop Examples Dim x As Integer = 1 Do While x < 5 Debug.Writeline(x.ToString()) x += 1 Loop Dim x As Integer = 1 Do Debug.Writeline(x.ToString()) x += 1 Loop Until x >= 5 For/Next Loops in Visual Basic Used to repeat statements in a loop a specific number of times. Uses a numeric counter variable, called a loop index, which is tested to determine the number of times the statements inside the loop will execute. The loop index is automatically incremented at the bottom of the loop on each iteration. Step can be included to specify the incrementing amount to increment the loop index. If Step is not included, it is assumed to be 1; Step can be a negative number. For Index [As Datatype] = Initial To End [Step Increment] 'Looping statements Next [LoopIndex] Do/Loop Examples For index As Integer = 6 To 10 Debug.Writeline(index.ToString()) Next index Dim index As Integer For index = 11 To 15 Debug.Writeline(index.ToString()) Next For index As Integer = 5 To 1 Step -1 Debug.Writeline(index.ToString()) Next index Exiting a Loop Abnormally In some situations, you may need to completely exit a loop before it has completed all iterations. Other times you may need to stay in the loop, but skip the current iteration and go onto the next one. To completely exit a loop early, use Exit Do for the Do/Loop statements Exit For for the For/Next statement To skip the current iteration of the loop and go onto the next iteration, use Continue Do for the Do/Loop statements Continue For for the For/Next statement 2 More Examples For index As Integer = 6 To Max If index > 100 Exit For End If Debug.Writeline(index.ToString()) Next index Dim x As Integer = 1 Do While x < 5 If x = 3 Then x += 1 Continue Do Endif Debug.Writeline(x.ToString()) x += 1 Loop Sending Information to the Printer Two steps required for adding print features to an application: The Easy Step: Add the components and call the methods that perform a Print or a Print Preview operation. The Hard Step: Build the page(s) of formatted output that will be printed or previewed. Many programmers use separate print utilities independent of Visual Studio for formatting the output to be printed. VB Professional Edition and Enterprise Edition include Crystal Reports for creating reports from database files. The Easy Step: Adding Print Components Components to add for printing appear in the Printing tab of the toolbox. Make sure you have buttons or menu items on your form for printing or preview printing. Drag either (or both) the PrintDocument component or the PrintPreviewDialog component to your Design Window. (They will appear below in the Component Tray The Easy Step: Adding Event Code In the event handler for your Print button or menu item, call the Print() method of the PrintDocument component: PrintDocument1.Print() In the event handler for your Print Preview button or menu item, add the following two lines of code: PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() Setting Up the Print Output Double-click on the PrintDocument component to expose the PrintPage event handler. A PrintPage event is fired once for each page to be printed, and is referred to as a callback. BeginPrint and EndPrint are two other events also fired at the beginning and end of the printing. The PrintPage event includes the argument e as System.Drawing.Printing.PrintPageEventArgs. Properties of the PrintPageEventArgs are useful for handling page margins and sending strings of text to the page. The Hard Step: Formatting the Print Page 1. Set up the font, line height, and (x,y) position of where to print the first line of the page. 2. For every line on the page: 2.1 Set up the text to be printed 2.2 Print the text 2.3 Increment the y coordinate for the next line Notes: Use properties of e (of type PrintPageEventArgs ) to obtain useful information about setting up the (x,y) position. Use the Graphics objects DrawString() method for displaying the text. The Page Coordinate System The units of measurement on the page are points (pixels). The X coordinate is the horizontal distance going across the page left to right. The Y coordinate is the vertical distance from the top of the page going down. Position (0,0) is the upper left corner of the drawing area Formatting Step 1: Set Up The PrintPageEventArgs argument, e, has several useful properties that are used to determine the (x,y) settings. Dim xLoc As Single Dim yLoc As Single xLoc = e.MarginBounds.Left yLoc = e.MarginBounds.Top Create a Font object to specify how the output looks: Dim PrintFont As New Font(Arial, 12) Dim LineHeight As Single = PrintFont.GetHeight + 2 Formatting Step 2: Printing the Line Use the Graphics method DrawString() to display a string (one line) of the information to be printed. DrawString() has the following general form: The Graphics object comes from the PrintPageEventsArg, e. Line is String, MyFon t is a Font object, MyBrush is a Brush object, and Xcoord and Ycoord are both Single. Example DrawString(Line, MyFont, MyBrush, Xcoord, Ycoord) e.Graphics.DrawString(Print this test string, PrintFont, Brushes.Black, xLoc, yLoc) Putting It All Together into Callback Private Sub PrintDocument1_PrintPage( ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim xLoc, yLoc As Single xLoc = e.MarginBounds.Left yLoc = e.MarginBounds.Top Dim PrintFont As New Font(Arial, 12) Dim LineHeight As Single = PrintFont.GetHeight + 2 e.Graphics.DrawString(Print test string, PrintFont, Brushes.Black, xLoc, yLoc) yLoc += LineHeight End Sub A Final Example (No Printing!) Write a Visual Basic application that lists all the ways you can order a hamburger with 3 toppings: Ketchup, Pickles & Onions. Each topping can be ordered 2 ways: with it or without it. So there are 2x2x2 = 2 3 = 8 different ways to order a hamburger with just 3 toppings. This example will use nested loops. A nested loop is one loop inserted inside of another- a common programming practice. We wont get fancy and include a form UI. Well just use the Intermediate Window for output. For ketchup As Integer = 0 To 1 For pickles As Integer = 0 To 1 For onions As Integer = 0 To 1 If ketchup = 0 Then Debug.WriteLine(Ketchup: No ) Else Debug.WriteLine(Ketchup: Yes ) End If If pickles = 0 Then Debug.WriteLine(Pickles: No ) Else Debug.WriteLine(Pickles: Yes ) End If If onions = 0 Then Debug.WriteLine(Onions: No ) Else Debug.WriteLine(Onions: Yes ) End If Debug.WriteLine(ControlChar.NewLine) Next onions Next pickles Next ketchup