Vb6 ListBox RemoveItem
You are here: Visual Basic > VB6 (Beginners Tutorial) Show Previous Page | Table of Contents | Next Page Using ListBox and ComboBox Controls In Visual Basic6ListBox 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.
Adding items to a ListIt 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 End Sub Removing Items from a ListThe 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() Sorting the ListThe 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 ComboBoxA 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.
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.
The following event procedures are entered for the TextBox and CommandButton controls. Private Sub txtName_Change() Private Sub cmdAdd_Click() 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() Private Sub cmdExit_Click() Private Sub cmdRemove_Click() remove = lstName.ListIndex 'Getting the index If remove >= 0 Then 'make sure an item is selected lstName.RemoveItem remove 'Remove item from the list box 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 |