VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)                                             Chapter 7List Box ControlA l...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)2. Simple Combo BoxSetting the Style property of a combo box to 1 – Simpl...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)Adding Items to list Box / Combo Box at Design-Time:   1. Place the list ...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)When a project is running, the user select an item from the list box/comb...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)Private Sub Command1_Click()        MsgBox Combo1.List(Combo1.ListIndex) ...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)The second form of Do/Loop tests for completion at the bottom of the loop...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)String Function    1. Left FunctionReturns a Variant (String) containing ...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)Right Function ExampleThis example uses the Right function to return a sp...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)    4. Len FunctionReturns a Long containing the number of characters in ...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)InStr Function ExampleThis example uses the InStr function to return the ...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)SAMPLE APPLICATIONPrivate Sub cmdAddCoffee_Click()  Add a new coffee flav...
VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)Private Sub mnuEditRemove_Click(Index As Integer)  Remove the selected co...
Upcoming SlideShare
Loading in...5
×

Vb6 ch.7-3 cci

350

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
350
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Vb6 ch.7-3 cci"

  1. 1. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI) Chapter 7List Box ControlA 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 ina single column.If the number of items exceeds what can be displayed in the list box, scroll bars automaticallyappear on the control. The user can then scroll up and down, or left to right through the list.Combo Box ControlA combo box control combines the features of a text box and a list box. This control allows theuser 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 bedisplayed in the combo box, scroll bars will automatically appear on the control. The user canthen scroll up and down or left to right through the list.Combo Box StylesThere are three combo box styles. Each style can be set at design time and uses values, orequivalent Visual Basic constants, to set the style of the combo box.Style Value ConstantDrop-down combo box 0 vbComboDropDownSimple combo box 1 vbComboSimpleDrop-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. 1
  2. 2. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)2. Simple Combo BoxSetting the Style property of a combo box to 1 – Simple Combo specifies a simple combobox in which the list is displayed at all times. To display all entries in the list, you must drawthe list box large enough to display the entries. A vertical scroll bar is automatically insertedwhen there are more entries than can be displayed. The user can still enter text directly orselect from the list. As with a drop-down combo box, a simple combo box also allows usersto enter choices not on the list.3. Drop-down List BoxA drop-down list box (Style = 2 – Dropdown List) is like a regular list box — it displays alist of items from which a user must choose. Unlike list boxes, however, the list is notdisplayed until you click the arrow to the right of the box. The key difference between thisand a drop-down combo box is that the user cant type into the box, he can only selectan item from the list. 2
  3. 3. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)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 Addtemmethod.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 thelist 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 thecombo box and list box.General form – Object.ClearExample: lstName.Clear cboName.ClearListIndex PropertyReturns or sets the index of the currently selected item in the control. Not available at designtime. 3
  4. 4. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)When a project is running, the user select an item from the list box/combo box, the index ofthat 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.ListIndexEnd SubListCount PropertyThe 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 SubPrivate Sub cmdOk_Click () MsgBox cboName.ListCountEnd SubList PropertyThe List Property is used to display a item from the list. The list property of list box/combo boxholds the text of all list elements.Specify the element by using its Index.General form –Object.List(Index) [ = Value]Example: 4
  5. 5. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)Private Sub Command1_Click() MsgBox Combo1.List(Combo1.ListIndex) ‘gets the current selected itemEnd SubRemoveItem PropertyThe 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 itemEnd SubDo/LoopsThe process of repeating a series of instructions is called Looping. The group of repeatedinstructions 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 continueswhile 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 iscalled as pretest. The statements inside the loop may never be executed if the terminatingcondition is true the first time it is tested.Syntax Do [{While | Until} condition] [statements] [Exit Do] [statements] Loop 5
  6. 6. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)The second form of Do/Loop tests for completion at the bottom of the loop. This type of loop iscalled 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 Descriptioncondition 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 StatementRepeats 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 DescriptionCounter 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.RemarksThe 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 thestatements in the loop execute again (based on the same test that caused the loop to executeinitially), or the loop is exited and execution continues with the statement following the Nextstatement. 6
  7. 7. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)String Function 1. Left FunctionReturns a Variant (String) containing a specified number of characters from the left side of a string.SyntaxLeft(string, length)The Left function syntax has these named arguments:Part Descriptionstring 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 ExampleThis example uses the Left function to return a specified number of characters from the leftside of a string.Dim AnyString, MyStrAnyString = "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 FunctionReturns a Variant (String) containing a specified number of characters from the right side of astring.SyntaxRight(string, length)The Right function syntax has these named arguments:Part Descriptionstring 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. 7
  8. 8. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)Right Function ExampleThis example uses the Right function to return a specified number of characters from the rightside of a string.Dim AnyString, MyStrAnyString = "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.SyntaxMid(string, start[, length])The Mid function syntax has these named arguments:Part Descriptionstring 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 ExampleThe first example uses the Mid function to return a specified number of characters from astring.Dim MyString, FirstWord, LastWord, MidWordsMyString = "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". 8
  9. 9. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI) 4. Len FunctionReturns a Long containing the number of characters in a string or the number of bytes requiredto store a variable.SyntaxLen(string | varname)The Len function syntax has these parts:Part Descriptionstring 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 ExampleThe first example uses Len to return the number of characters in a string or the number of bytesrequired to store a variable.Dim MyString, MyLenMyString = "Hello World" Initialize variable.MyLen = Len(MyString) Returns 11. 5. InStr FunctionReturns 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 Descriptionstart 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. 9
  10. 10. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)InStr Function ExampleThis example uses the InStr function to return the position of the first occurrence of one stringwithin another.Dim SearchString, SearchChar, MyPosSearchString ="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).SyntaxLTrim(string)RTrim(string)Trim(string)The required string argument is any valid string expression. If string contains Null, Null isreturned.LTrim, RTrim, and Trim Functions ExampleThis example uses the LTrim function to strip leading spaces and the RTrim function to striptrailing spaces from a string variable. It uses the Trim function to strip both types of spaces.Dim MyString, TrimStringMyString = " <-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->". 10
  11. 11. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)SAMPLE APPLICATIONPrivate 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.SetFocusEnd SubPrivate Sub mnuEditAdd_Click(Index As Integer) Add a new coffee to list cmdAddCoffee_ClickEnd SubPrivate 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 IfEnd SubPrivate Sub mnuEditCount_Click(Index As Integer) Display a count of the coffee list MsgBox "The number of coffee types is " & cboCoffee.ListCountEnd Sub 11
  12. 12. VISUAL BASIC 6 – 3 CUBE COMPUTER INSTITUTE (3CCI)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 IfEnd SubPrivate Sub mnuFileExit_Click(Index As Integer) Terminate the Project EndEnd SubPrivate 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.EndDocEnd SubPrivate 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 IfEnd Sub 12

×