Vb6 ListBox RemoveItem

You are here: Visual Basic > VB6 [Beginners Tutorial]

Previous Page | Table of Contents | Next Page

Using ListBox and ComboBox Controls In Visual Basic6

ListBox and ComboBox controls present a set of choices that are displayed vertically in a column. If the number of items exceed the value that be displayed, scroll bars will automatically appear on the control. These scroll bars can be scrolled up and down or left to right through the list.

The following Fig lists some of the common ComboBox properties and methods.

Property/Method
Description

Properties

EnabledBy setting this property to True or False user can decide whether user can interact with this control or not
IndexSpecifies the Control array index
ListString array. Contains the strings displayed in the drop-down list. Starting array index is 0.
ListCountInteger. Contains the number of drop-down list items
ListIndexInteger. Contains the index of the selected ComboBox item. If an item is not selected, ListIndex is -1
LockedBoolean. Specifies whether user can type or not in the ComboBox
MousePointerInteger. Specifies the shape of the mouse pointer when over the area of the ComboBox
NewIndexInteger. Index of the last item added to the ComboBox. If the ComboBox does not contain any items , NewIndex is -1
SortedBoolean. Specifies whether the ComboBox's items are sorted or not.
StyleInteger. Specifies the style of the ComboBox's appearance
TabStopBoolean. Specifies whether ComboBox receives the focus or not.
TextString. Specifies the selected item in the ComboBox
ToolTipIndexString. Specifies what text is displayed as the ComboBox's tool tip
VisibleBoolean. Specifies whether ComboBox is visible or not at run time

Methods

AddItemAdd an item to the ComboBox
ClearRemoves all items from the ComboBox
RemoveItemRemoves the specified item from the ComboBox
SetFocusTransfers focus to the ComboBox

Event Procedures

ChangeCalled when text in ComboBox is changed
DropDownCalled when the ComboBox drop-down list is displayed
GotFocusCalled when ComboBox receives the focus
LostFocusCalled when ComboBox loses it focus

Adding items to a List

It is possible to populate the list at design time or run time

Design Time : To add items to a list at design time, click on List property in the property box and then add the items. Press CTRL+ENTER after adding each item as shown below.

Run Time : The AddItem method is used to add items to a list at run time. The AddItem method uses the following syntax.

Object.AddItemitem, Index

The item argument is a string that represents the text to add to the list

The index argument is an integer that indicates where in the list to add the new item. Not giving the index is not a problem, because by default the index is assigned.

Following is an example to add item to a combo box. The code is typed in the Form_Load event

Private Sub Form_Load[]

Combo1.AddItem 1
Combo1.AddItem 2
Combo1.AddItem 3
Combo1.AddItem 4
Combo1.AddItem 5
Combo1.AddItem 6

End Sub

Removing Items from a List

The RemoveItem method is used to remove an item from a list. The syntax for this is given below.

Object.RemoveItem index

The following code verifies that an item is selected in the list and then removes the selected item from the list.

Private Sub cmdRemove_Click[]
If List1.ListIndex > -1 Then
List1.RemoveItem List1. ListIndex
End If
End Sub

Sorting the List

The Sorted property is set to True to enable a list to appear in alphanumeric order and False to display the list items in the order which they are added to the list.

Using the ComboBox

A ComboBox combines the features of a TextBox and a ListBox. This enables the user to select either by typing text into the ComboBox or by selecting an item from the list. There are three types of ComboBox styles that are represented as shown below.

Dropdown combo

Simple combo

Dropdown list

  • Dropdown Combo [style 0]

  • Simple Combo [style 1]

  • Dropdown List [style 2]

The Simple Combo box displays an edit area with an attached list box always visible immediately below the edit area. A simple combo box displays the contents of its list all the time. The user can select an item from the list or type an item in the edit box portion of the combo box. A scroll bar is displayed beside the list if there are too many items to be displayed in the list box area.

The Dropdown Combo box first appears as only an edit area with a down arrow button at the right. The list portion stays hidden until the user clicks the down-arrow button to drop down the list portion. The user can either select a value from the list or type a value in the edit area.

The Dropdown list combo box turns the combo box into a Dropdown list box. At run time , the control looks like the Dropdown combo box. The user could click the down arrow to view the list. The difference between Dropdown combo & Dropdown list combo is that the edit area in the Dropdown list combo is disabled. The user can only select an item and cannot type anything in the edit area. Anyway this area displays the selected item.

Example

This example is to Add , Remove, Clear the list of items and finally close the application.

  • Open a new Standard EXE project is opened an named the Form as Listbox.frm and save the project as Listbox.vbp

  • Design the application as shown below.

Object
Property
Settings
Form

Caption

Name

ListBox

frmListBox

TextBox

Text

Name

[empty]

txtName

Label

Caption

Name

Enter a name

lblName

ListBoxNamelstName
Label

Caption

Name

Amount Entered

lblAmount

Label

Caption

Name

Border Style

[empty]

lblDisplay

1 Fixed Single

CommandButton

Caption

Name

Add

cmdAdd

CommandButton

Caption

Name

Remove

cmdRemove

CommandButton

Caption

Name

Clear

cmdClear

CommandButton

Caption

Name

Exit

cmdExit

The following event procedures are entered for the TextBox and CommandButton controls.

Private Sub txtName_Change[]
If [Len[txtName.Text] > 0] Then 'Enabling the Add button
'if atleast one character
'is entered
cmdAdd.Enabled = True
End If
End Sub

Private Sub cmdAdd_Click[]
lstName.AddItem txtName.Text 'Add the entered the characters to the list box

txtName.Text = "" 'Clearing the text box

txtName.SetFocus 'Get the focus back to the
'text box

lblDisplay.Caption = lstName.ListCount 'Display the number of items in the list box

cmdAdd.Enabled = False ' Disabling the Add button

End Sub

The click event of the Add button adds the text to the list box that was typed in the Text box. Then the text box is cleared and the focus is got to the text box. The number of entered values will is increased according to the number of items added to the listbox.

Private Sub cmdClear_Click[]
lstName.Clear
lblDisplay.Caption = lstName.ListCount
End Sub

Private Sub cmdExit_Click[]
Unload Me
End Sub

Private Sub cmdRemove_Click[]
Dim remove As Integer

remove = lstName.ListIndex 'Getting the index

If remove >= 0 Then 'make sure an item is selected
'in the list box

lstName.RemoveItem remove 'Remove item from the list box

lblDisplay.Caption = lstName.ListCount 'Display the number of items
'in the listbox

End If

End Sub

Remove button removes the selected item from the list as soon as you pressed the Remove button. The number of items is decreased in the listbox and the value is displayed in the label.

The code for the clear button clears the listbox when you press it. And the number of items shown in the label becomes 0.

[ Download the source code ]


Previous Page | Table of Contents | Next Page

Video liên quan

Chủ Đề