Using String Functions Left(str, nchars) - extract left nchars Right(str, nchars) - extract right...

Post on 20-Dec-2015

215 views 0 download

Transcript of Using String Functions Left(str, nchars) - extract left nchars Right(str, nchars) - extract right...

Using String Functions

• Left(str, nchars) - extract left nchars

• Right(str, nchars) - extract right nchars

• Mid(str,start,[nchars]) - get middle chars

• str is any string, nchars is integer, and start is integer number of starting position.

• Len(str) returns # chars in string

String Functions Example

•Suppose a text box named txtName contains the following:

•Left(txtName.Text, 3) is “Wha” (without the quotation marks)

•Right(txtName.Text, 3) is “ob?” (without the quotation marks)

•Mid(txtName.Text, 6, 5) is “about” (without the quotation marks)

•Mid (txtname.Text, 6) is “about Bob?” (without the quotation marks)

What about Bob?Text property of textbox called txtName

Using String Functions (2)

• String functions and looping facilitate the “zooming” feature of many Microsoft list boxes and combo boxes.

• “Zooming” causes a search of the list, refining the search with each additional keystroke the user enters.

Sending Info. to the Printer• Visual Basic’s Print method applies to a

Form, a printer object, the Debug window, or a picture box

• Printer.Print — sends output to printer

• Print is a method of the Printer object

• Printing is somewhat difficult, because VB is designed to use with forms, not printers

Formatting Lines

• Printer.print x, y, z prints in three "zones"

• Printer.print x;y;z ignores zones

• Trailing comma or semicolon causes next print method to continue the line

• Each .print starts on a new line• Printer.print (alone) prints blank line

Formatting Lines• Tab(20) advances to 20th position:

Printer.Print Tab(20); "Name:"

• Spc(17) inserts spaces (17) in line

• Numbers print with one space for sign of #

• Printer.NewPage sends page to printer & goes to new page

• Printer.EndDoc sends page to printer & halts print job

Printing to Picture Box

• You can use Print method with a picture box to avoid actually printing a result:

• picMyPictureBox.Print "howdy" prints to a picture box

List Boxes & Combo Boxes

• List/Combo boxes allow a list of items• Types include

– simple combo boxes– dropdown combo boxes– dropdown lists

• Combo box has style property; list does not

• Combo box has design-time text property; list box has only run-time text property.

• Combo box does not have a MaxLength property; a list box does.

• If the list is empty, then the ListCount is zero• You can deselect all items in a list by setting the ListIndex property

to -1 • Set the ListIndex property to 0 to select (highlight) the first item in

the list--remember that indexes run from 0 to n-1• ListCount holds the number of items in a list. Remember that

ListCount is always one higher than the highest ListIndex.• Display one of the items in a list by selecting the list object and

referring to the ListIndex value as an index:– lstBookTypes(iValue)

• Add items to the list in code with:– lstSchools.AddItem “Purdue”– lstSchools.AddItem “University of San Diego”

• The Sorted property of the list keeps items in alphabetic order.• Copy selected list item to a text box:

– txtSelection.text = cboFruit.List(3)– txtSelection.text = cboFruit.List(cboFruit.ListIndex)

• Setting a list index to a value selects—highlights—the list item:– lstCoffeeTypes.ListIndex = 3 ‘Highlight the 4th item in list– lstCoffeeTypes.ListIndex = -1 'Deselect all in list

list boxes

simple combo box (style 1)

dropdown combo box (style 1)

dropdown list (style 2)

List property

design-time contents of list

Filling the List

• You can fill the list at design- or run-time• Fill at design time if list rarely changes

– Click List property, type entry, press Ctrl+Enter– Make Sorted property true to keep list in order

• Fill at run time if list is volatile– Use AddItem method to add items– Form: object.AddItem value [,Index]– Example: lstFruits.AddItem "Apple"

List/Combo Box Methods

• AddItem - Add item to listcboFruits.AddItem "Kiwi"

cboFruits.AddItem cboFruits.Text

• Clear - Delete all items from the listcboFruits.Clear

List/Combo Box Properties

• Sorted maintains list in order (if true)

• ListIndex holds user-selected item index number (begins at zero, -1 if none selected)

• ListCount is number of items in list

• List object.List(index) extracts string value of list item

• Setting the ListIndex property highlights a selection in the list

List/Combo box Events

• Change Occurs when user types text into combo box (list box does not have this)

• GotFocus Occurs when control receives control

• LostFocus Occurs when control loses control