Monday, 10 September 2012

VB List Box



List box is one of the intrinsic controls. List box is used to allow user to select one or more of the given list of items.  A List box can contain any number of items, but the number of items that a List box can display depends upon the display size of the List box. If List box contains more items than what can be displayed then automatically a scrollbar is attached to List box. 

Now, let us develop a small program to understand how to handle a List box. See figure 5.1, for the user interface of the program.


Figure 5.1: List box demo program at runtime.

Here are the required steps to create a new project and  user interface.

1.      Start a new project using File->New Project
2.      Select Standard Exe in list of project types and click on Ok.
3.      Place the required control on the form and arrange them as shown in figure 5.1.
4.      Change the properties as follows.
Control
Property

Value

Form1
Name
Frmlist

Caption
&List box Demo
Label1
Name
Lblnames

Caption
Names
List1
Name
Lstnames
Lable2
Name
Lblname

Caption
&Name
Text1
Name
Txtname

Text
(null string)
Command1
Name
Cmdadd

Caption
&Add Name
Command2
Name
Cmddelete

Caption
&Delete Name
Command3
Name
Cmdquit

Caption
&Quit Program
Label3
Name
Lblcount

Caption
# Names : 0


Now let us write code. We have to write code for Click event of three command buttons. We first concentrate on how to add an item to List box and how to delete an item from List box.

Adding an item to List box
You add an item to List box using Additem method of the List box. Additem method takes an item, which is a string, and adds that item at the end of the List box. It is also possible to add an item at the specified index using index parameter of Additem method.

The following is the syntax of additem method.

AddItem item, index


Object
 Is any List box.
Item  
 Is the item to be added to List box. This is a string expression.
Index
 It is optional. If it is given then item is added at the specified position. Index starts from 0

Example:

To add name P.SRIKANTH to List box, enter:

List1.additem “P.SRIKANTH”

To add an item at 2nd position in the List box, enter:

List1.additem “Praneeth”, 2

Removing an item from List box
You can remove an item from List box using Removeitem method of the List box. Removeitem method takes the index of the item to be removed.

Here is the syntax of Removeitem method of List box:

RemoveItem index

Index
The index of the item that is to be removed from List box. Index must be in the range 0 to ListCount –1. ListCount property contains the number of items in the List Box.

To remove item at 1st position (second item) of List box, enter:

List1.removeitem  1


Writing Code for cmdadd and cmddelete buttons
Now let us write code to add the name entered in textbox to List box. The code is written for Click event of add (cmdadd) button. After name is added to List box the name is removed from textbox and focus is set to textbox so that a new name can be entered by user. As this change effects the count of items in the List box, update names count by changing Caption property of lblcount.

Private Sub cmdadd_Click()
 ' add item to List box
 lstnames.AddItem txtname.Text
 'update # items display
 lblcount.Caption = "# Names : " & Str(lstnames.ListCount)
 ' remove text from textbox
 txtname.Text = ""
 ' set focus to textbox
 txtname.SetFocus
End Sub
Listing 5.1: Code for adding an item.

Now let us write code to delete the item that is currently selected in the List box. To remove an item from the List box, use Removeitem method of the List box. Removeitem method takes index of the item to be removed. As we have to delete currently selected item, we use the index of the item that is currently selected.  ListIndex property returns the index of the item that is currently selected. Finally update count of items as deletion of an item decrements the count.

Private Sub cmddelete_Click()

 'attempt to delete only when an item is selected
 If lstnames.ListIndex >= 0 Then
  ' delete the item that is currently selected
  lstnames.RemoveItem lstnames.ListIndex
  lblcount.Caption = "# Names : " & Str(lstnames.ListCount)
 Else  'no item is currently selected
  Beep
 End If
End Sub
Listing 5.2: Code for deleting the current item of List box.

Test Run
Follow the steps given below to test whether the program is doing what it is supposed to do.

1.      Run program by pressing F5.
 
Properties of List box
The following table lists properties of List box. We have already used LISTINDEX and LISTCOUNT properties of List box in the previous program.
 
Property

Meaning

Listindex
Contains the index of the item that is currently selected.
Listcount
Contains the number of items the List box currently has.
Text
Contains the item that is currently selected.
Sorted
If set to true, items in the List box are always arranged in the ascending order.
List( )
This is an array. Each element contains an item of the List box. List(0) refers to first item, list(1) refers to second item and so on. The last element that can be accessed is LISTCOUNT –1.
Style
Determines the style of the List box. If it is set to 0-standard, then items are displayed as text items. If it is set to 1-checkbox, then items are displayed with a checkbox on the left. Multiple items can be selected by checking the checkbox displayed on the left of the item.
Columns
Sets a value that determines whether a List box scrolls vertically or horizontally and how the items in the columns are displayed.  If it is set to 0, then only one column is displayed and List box scrolls vertically. If it is set to>= 1 then items are arranged in snaking columns, filling the first column, then the second column, and so on. The List box scrolls horizontally and displays the specified number of columns.

No comments:

Post a Comment