vb6 ch.7-3 cci

12
VISUAL BASIC 6 3 CUBE COMPUTER INSTITUTE (3CCI) 1 Chapter 7 List Box Control A list box control displays a list of items from which the user can select one or more. List boxes present a list of choices to the user. By default, the choices are displayed vertically in a single column. If the number of items exceeds what can be displayed in the list box, scroll bars automatically appear on the control. The user can then scroll up and down, or left to right through the list. Combo Box Control A combo box control combines the features of a text box and a list box. This control allows the user to select an item either by typing text into the combo box, or by selecting it from the list. Combo boxes present a list of choices to the user. If the number of items exceeds what can be displayed in the combo box, scroll bars will automatically appear on the control. The user can then scroll up and down or left to right through the list. Combo Box Styles There are three combo box styles. Each style can be set at design time and uses values, or equivalent Visual Basic constants, to set the style of the combo box. Style Value Constant Drop-down combo box 0 vbComboDropDown Simple combo box 1 vbComboSimple Drop-down list box 2 vbComboDropDownList 1. Drop-down Combo Box With the default setting (Style = 0 Dropdown Combo), a combo box is a drop-down combo box. The user can either enter text directly (as in a text box) or click the detached arrow at the right of the combo box to open a list of choices. Selecting one of the choices inserts it into the text portion at the top of the combo box.

Upload: fahim-khan

Post on 20-Jun-2015

350 views

Category:

Entertainment & Humor


0 download

TRANSCRIPT

Page 1: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

1

Chapter 7 List Box Control A list box control displays a list of items from which the user can select one or more.

List boxes present a list of choices to the user. By default, the choices are displayed vertically in a single column. If the number of items exceeds what can be displayed in the list box, scroll bars automatically appear on the control. The user can then scroll up and down, or left to right through the list. Combo Box Control

A combo box control combines the features of a text box and a list box. This control allows the user to select an item either by typing text into the combo box, or by selecting it from the list.

Combo boxes present a list of choices to the user. If the number of items exceeds what can be displayed in the combo box, scroll bars will automatically appear on the control. The user can then scroll up and down or left to right through the list.

Combo Box Styles There are three combo box styles. Each style can be set at design time and uses values, or equivalent Visual Basic constants, to set the style of the combo box.

Style Value Constant

Drop-down combo box 0 vbComboDropDown

Simple combo box 1 vbComboSimple

Drop-down list box 2 vbComboDropDownList

1. Drop-down Combo Box With the default setting (Style = 0 – Dropdown Combo), a combo box is a drop-down combo box. The user can either enter text directly (as in a text box) or click the detached arrow at the right of the combo box to open a list of choices. Selecting one of the choices inserts it into the text portion at the top of the combo box.

Page 2: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

2

2. Simple Combo Box Setting the Style property of a combo box to 1 – Simple Combo specifies a simple combo box in which the list is displayed at all times. To display all entries in the list, you must draw the list box large enough to display the entries. A vertical scroll bar is automatically inserted when there are more entries than can be displayed. The user can still enter text directly or select from the list. As with a drop-down combo box, a simple combo box also allows users to enter choices not on the list.

3. Drop-down List Box A drop-down list box (Style = 2 – Dropdown List) is like a regular list box — it displays a list of items from which a user must choose. Unlike list boxes, however, the list is not displayed until you click the arrow to the right of the box. The key difference between this and a drop-down combo box is that the user can't type into the box, he can only select an item from the list.

Page 3: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

3

Adding Items to list Box / Combo Box at Design-Time:

1. Place the list box/ Combo box in form and choose the list box/ Combo box. 2. Go to list property of List Box Control/ Combo box Control. 3. Add 1st item to the list box/ Combo box. 4. To add 2nd item press Ctrl + Enter. 5. Repeat 4th step for adding more items to the list. 6. After Last item is added, don’t press Ctrl + Enter, otherwise at the end of list box/ Combo

box an empty string will be added.

Adding Items to list Box / Combo Box at Run-Time: If you want to add item in the list box/ Combo Box during runtime, you have to use the Addtem method. General form – Object.AddItem Value [, Index] Value is the value to add to the list. If the value is a string literal, enclose it in quotation marks. Index specifies the position in the list where the value must be stored, the first element in the list box has the Index = 0. Example: lstName.AddItem “A” cboName.AddItem “B” Clearing The List Box/ Combo Box Items: To clear items of the list box or a combo box at runtime, use the Clear Method to empty the combo box and list box. General form – Object.Clear Example: lstName.Clear cboName.Clear

ListIndex Property

Returns or sets the index of the currently selected item in the control. Not available at design time.

Page 4: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

4

When a project is running, the user select an item from the list box/combo box, the index of that selected item is stored in the ListIndex property. ListIndex of the first item in the list is 0. If no list item is selected then the ListIndex property is set to -1. Example: Private Sub cmdOk_Click () MsgBox cboName.ListIndex End Sub

ListCount Property The ListCount property of a list box/combo box is used to store the number of items in the list. ListCount is always one more that the highest ListIndex, since ListIndex starts from 0. Example: Private Sub Form_Load()

Combo1.AddItem "A" Combo1.AddItem "B" Combo1.AddItem "C" Combo1.AddItem "D"

End Sub Private Sub cmdOk_Click () MsgBox cboName.ListCount End Sub

List Property The List Property is used to display a item from the list. The list property of list box/combo box holds the text of all list elements. Specify the element by using its Index. General form –Object.List(Index) [ = Value] Example:

Page 5: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

5

Private Sub Command1_Click() MsgBox Combo1.List(Combo1.ListIndex) ‘gets the current selected item

End Sub

RemoveItem Property The RemoveItem Property is used to remove a single element from the list. General form – Object.RemoveItem Index. The index is required as it specifies which item is to be removed. Example: Private Sub Command1_Click()

Combo1.RemoveItem Combo1.ListIndex ‘removes the current selected item End Sub

Do/Loops The process of repeating a series of instructions is called Looping. The group of repeated instructions is called a loop. An iteration is a single execution of the statements in the loop. A Do/Loop terminates on the condition that you specify. Execution of a Do/Loop continues while a condition is True or until a condition is true. The first form of the Do/loop tests for completion at the top of the loop. This type of loop is called as pretest. The statements inside the loop may never be executed if the terminating condition is true the first time it is tested. Syntax

Do [{While | Until} condition] [statements] [Exit Do] [statements] Loop

Page 6: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

6

The second form of Do/Loop tests for completion at the bottom of the loop. This type of loop is called as posttest. Which means the statements of the loop will always be executed at least once. Syntax

Do [statements] [Exit Do] [statements] Loop [{While | Until} condition]

The Do Loop statement syntax has these parts:

Part Description

condition Optional. Numeric expression or string expression that is True or False. If condition is Null, condition is treated as False.

statements One or more statements that are repeated while, or until, condition is True.

For...Next Statement Repeats a group of statements a specified number of times. Syntax

For counter = start To end [Step step] [statements] [Exit For] [statements] Next [counter]

The For…Next statement syntax has these parts:

Part Description

Counter Required. Numeric variable used as a loop counter.

start Required. Initial value of counter.

end Required. Final value of counter.

step Optional. Amount counter is changed each time through the loop. If not specified, step defaults to one.

statements Optional. One or more statements between For and Next that are executed the specified number of times.

Remarks The step argument can be either positive or negative. After all statements in the loop have executed, step is added to counter. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the Next statement.

Page 7: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

7

String Function

1. Left Function Returns a Variant (String) containing a specified number of characters from the left side of a string. Syntax Left(string, length) The Left function syntax has these named arguments:

Part Description

string Required. String expression from which the leftmost characters are returned. If string contains Null, Null is returned.

length Required; Variant (Long). Numeric expression indicating how many characters to return. If 0, a zero-length string ("") is returned. If greater than or equal to the number of characters in string, the entire string is returned.

Left Function Example

This example uses the Left function to return a specified number of characters from the left

side of a string.

Dim AnyString, MyStr

AnyString = "Hello World" ' Define string.

MyStr = Left(AnyString, 1) ' Returns "H".

MyStr = Left(AnyString, 7) ' Returns "Hello W".

MyStr = Left(AnyString, 20) ' Returns "Hello World".

2. Right Function

Returns a Variant (String) containing a specified number of characters from the right side of a string.

Syntax

Right(string, length)

The Right function syntax has these named arguments:

Part Description

string Required. String expression from which the rightmost characters are returned. If

string contains Null, Null is returned.

length Required; Variant (Long). Numeric expression indicating how many characters to

return. If 0, a zero-length string ("") is returned. If greater than or equal to the

number of characters in string, the entire string is returned.

Page 8: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

8

Right Function Example

This example uses the Right function to return a specified number of characters from the right side of a string.

Dim AnyString, MyStr AnyString = "Hello World" ' Define string. MyStr = Right(AnyString, 1) ' Returns "d". MyStr = Right(AnyString, 6) ' Returns " World". MyStr = Right(AnyString, 20) ' Returns "Hello World".

3. Mid Function

Returns a Variant (String) containing a specified number of characters from a string.

Syntax

Mid(string, start[, length])

The Mid function syntax has these named arguments:

Part Description

string Required. String expression from which characters are returned. If string contains

Null, Null is returned.

start Required; Long. Character position in string at which the part to be taken begins. If

start is greater than the number of characters in string, Mid returns a zero-length

string ("").

length Optional; Variant (Long). Number of characters to return. If omitted or if there are

fewer than length characters in the text (including the character at start), all

characters from the start position to the end of the string are returned.

Mid Function Example

The first example uses the Mid function to return a specified number of characters from a string.

Dim MyString, FirstWord, LastWord, MidWords MyString = "Mid Function Demo" ' Create text string. FirstWord = Mid(MyString, 1, 3) ' Returns "Mid". LastWord = Mid(MyString, 14, 4) ' Returns "Demo". MidWords = Mid(MyString, 5) ' Returns "Function Demo".

Page 9: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

9

4. Len Function

Returns a Long containing the number of characters in a string or the number of bytes required to store a variable.

Syntax

Len(string | varname)

The Len function syntax has these parts:

Part Description

string Any valid string expression. If string contains Null, Null is returned.

Varname Any valid variable name. If varname contains Null, Null is returned. If varname is

a Variant, Len treats it the same as a String and always returns the number of

characters it contains.

Len Function Example

The first example uses Len to return the number of characters in a string or the number of bytes required to store a variable.

Dim MyString, MyLen MyString = "Hello World" ' Initialize variable. MyLen = Len(MyString) ' Returns 11.

5. InStr Function

Returns a Variant (Long) specifying the position of the first occurrence of one string within another. Syntax: InStr([start, ]string1, string2)

The InStr function syntax has these arguments:

Part Description

start Optional. Numeric expression that sets the starting position for each search. If

omitted, search begins at the first character position. If start contains Null, an

error occurs. The start argument is required if compare is specified.

string1 Required. String expression being searched.

string2 Required. String expression sought.

Page 10: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

10

InStr Function Example This example uses the InStr function to return the position of the first occurrence of one string within another. Dim SearchString, SearchChar, MyPos SearchString ="XXpXXpXXPXXP" ' String to search in. SearchChar = "P" ' Search for "P". ' A textual comparison starting at position 4. Returns 6. MyPos = Instr(4, SearchString, SearchChar, 1) ' A binary comparison starting at position 1. Returns 9. MyPos = Instr(1, SearchString, SearchChar, 0) ' Comparison is binary by default (last argument is omitted). MyPos = Instr(SearchString, SearchChar) ' Returns 9. MyPos = Instr(1, SearchString, "W") ' Returns 0.

6. LTrim, RTrim, and Trim Functions

Returns a Variant (String) containing a copy of a specified string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).

Syntax LTrim(string) RTrim(string) Trim(string) The required string argument is any valid string expression. If string contains Null, Null is returned. LTrim, RTrim, and Trim Functions Example This example uses the LTrim function to strip leading spaces and the RTrim function to strip trailing spaces from a string variable. It uses the Trim function to strip both types of spaces. Dim MyString, TrimString MyString = " <-Trim-> " ' Initialize string. TrimString = LTrim(MyString) ' TrimString = "<-Trim-> ". TrimString = RTrim(MyString) ' TrimString = " <-Trim->". TrimString = LTrim(RTrim(MyString)) ' TrimString = "<-Trim->". ' Using the Trim function alone achieves the same result. TrimString = Trim(MyString) ' TrimString = "<-Trim->".

Page 11: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

11

SAMPLE APPLICATION

Private Sub cmdAddCoffee_Click() 'Add a new coffee flavor to the coffee list If cboCoffee.Text <> "" Then With cboCoffee .AddItem .Text .Text = "" End With Else MsgBox "Entere a coffee name to add", vbExclamation, "Missing Data" End If cboCoffee.SetFocus End Sub Private Sub mnuEditAdd_Click(Index As Integer) 'Add a new coffee to list cmdAddCoffee_Click End Sub Private Sub mnuEditClear_Click(Index As Integer) 'Clear the coffee list Dim intResponse As Integer intResponse = MsgBox("Clear the coffee flavor list?", _ vbYesNo + vbQuestion, "Clear coffee list") If intResponse = vbYes Then cboCoffee.Clear End If End Sub Private Sub mnuEditCount_Click(Index As Integer) 'Display a count of the coffee list MsgBox "The number of coffee types is " & cboCoffee.ListCount End Sub

Page 12: Vb6 ch.7-3 cci

VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)

12

Private Sub mnuEditRemove_Click(Index As Integer) 'Remove the selected coffee from list If cboCoffee.ListIndex <> -1 Then cboCoffee.RemoveItem cboCoffee.ListIndex Else MsgBox "First select the coffee to remove", vbInformation, _ "No selection made" End If End Sub Private Sub mnuFileExit_Click(Index As Integer) 'Terminate the Project End End Sub Private Sub mnuFilePrintAll_Click(Index As Integer) 'Print the contents of the coffee flavors combo box on the printer Dim intIndex As Integer Dim intFinalValue As Integer Printer.Print 'Blank Line Printer.Print Tab(20); "Coffee Flavors" Printer.Print 'Blank Line intFinalValue = cboCoffee.ListCount - 1 'List index starts at 0 For intIndex = 0 To intFinalValue Printer.Print Tab(20); cboCoffee.List(intIndex) Next intIndex Printer.EndDoc End Sub Private Sub mnuFilePrintSelect_Click(Index As Integer) 'Send the current selection of coffee flavor 'and syrup flavor to the printer If cboCoffee.ListIndex <> -1 And lstSyrup.ListIndex <> -1 Then Printer.Print 'Blank Line Printer.Print Tab(15); "Coffee Selection" Printer.Print 'Blank Line Printer.Print Tab(10); "Coffee Flavor: "; cboCoffee.Text Printer.Print 'Blank Line Printer.Print Tab(10); "Syrup Flavor: "; lstSyrup.Text Else MsgBox "Make a selection for coffee and syrup.", vbExclamation, _ "Missing Data" End If End Sub