Visual Basic ListBox add item

I'm using vb.net forms

I'm trying to add items to a listbox one by one from many comboboxes and textboxes

the problem is when i add the second item to the listbox it replace the first one

i want the listbox to keep the first item and add a new one

here is my code

Private Sub Button39_Click[sender As Object, e As EventArgs] Handles Button39.Click ListBox1.Items.Add[ComboBox1.Text.ToString[] & " " & ComboBox2.Text.ToString[] & " " & textbox1.Text.ToString[]]

6

  • Article
  • 09/13/2021
  • 2 minutes to read

Adds a new item to the list of values displayed by the specified list box control.

Syntax

expression.AddItem [Item, Index]

expression A variable that represents a ListBox object.

Parameters

Name Required/Optional Data type Description
Item Required String The display text for the new item.
Index Optional Variant The position of the item in the list. If this argument is omitted, the item is added to the end of the list.

The RowSourceType property of the specified control must be set to Value List.

This method is only valid for list box or combo box controls on forms.

List item numbers start from zero. If the value of the Item argument doesn't correspond to an existing item number, an error occurs.

For multiple-column lists, use semicolons to delimit the strings for each column [for example, "1010;red;large" for a three-column list]. If the Item argument contains fewer strings than columns in the control, items will be added starting with the left-most column. If the Item argument contains more strings than columns in the control, the extra strings are ignored.

Use the RemoveItem method to remove items from the list of values.

Example

This example adds an item to the end of the list in a list box control. For the function to work, you must pass it a ListBox object representing a list box control on a form and a String value representing the text of the item to be added.

Function AddItemToEnd[ctrlListBox As ListBox, _ ByVal strItem As String] ctrlListBox.AddItem Item:=strItem End Function

This example adds an item to the beginning of the list in a combo box control. For the function to work, you must pass it a ComboBox object representing a combo box control on a form and a String value representing the text of the item to be added.

Function AddItemToBeginning[ctrlComboBox As ComboBox, _ ByVal strItem As String] ctrlComboBox.AddItem Item:=strItem, Index:=0 End Function

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

  • Article
  • 09/13/2021
  • 2 minutes to read

The following example adds and deletes the contents of a ListBox using the AddItem and RemoveItem methods, and the ListIndex and ListCount properties.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

  • A ListBox named ListBox1.
  • Two CommandButton controls named CommandButton1 and CommandButton2.
Dim EntryCount As Single Private Sub CommandButton1_Click[] EntryCount = EntryCount + 1 ListBox1.AddItem [EntryCount & " - Selection"] End Sub
Private Sub CommandButton2_Click[] 'Ensure ListBox contains list items If ListBox1.ListCount >= 1 Then 'If no selection, choose last list item. If ListBox1.ListIndex = -1 Then ListBox1.ListIndex = _ ListBox1.ListCount - 1 End If ListBox1.RemoveItem [ListBox1.ListIndex] End If End Sub
Private Sub UserForm_Initialize[] EntryCount = 0 CommandButton1.Caption = "Add Item" CommandButton2.Caption = "Remove Item" End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.

The "AddItem" function in Visual Basic 6.0 lets you dynamically add items to the list of options from which users can choose. ListBox controls display a drop-down list of options, and your users choose only one option to submit in your Web or desktop form. You can add one or several items programmatically in the VB6 language.

  1. Click the Windows "Start" button and choose "All Programs" from the menu. Click "Microsoft Visual Basic," and then click "Visual Basic 6" to open the compiler.

  2. Open the VB6 project you want to edit. After the project loads, double-click the form that contains your ListBox. Right-click the opened form and click "View Code" to open the coding editor.

  3. Scroll down the code to where you want to add an item. Use the following code to add an item to the ListBox:

    ListBox1.AddItem "New Option"

    Replace "ListBox1" with your own ListBox control name. Replace "New Option" with the option name you want to display to your users.

In Visual Basic.Net, items can also be added at runtime using the Add[ ] method. We can apply this method in Visual Basic 2017, Visual Basic 2015, Visual Basic 2013, Visual Basic 2012, Visual Basic 2010 as well as Visual Basic 2008.

The syntax of the Add[] method is as follows:

Private Sub Button1_Click[sender As Object, e As EventArgs] Handles Button1.Click ListBox1.Items.Add[text] End Sub

Example:

Private Sub Button1_Click[sender As Object, e As EventArgs] Handles Button1.Click
     ListBox1.Items.Add[“Apple”]
End Sub

The Output is as shown below:

Besides that, we can also allow the user to add items via a popup input box.

In the following example, we create a variable myitem and then assign a value to myitem via the InputBox function that store the input from the user. We then use the Add[] method to add the user’s item into the listbox. The code is as follows:

Private Sub Button1_Click[sender As Object, e As EventArgs] Handles Button1.Click Dim myitem 'declare the variable myitem myitem = InputBox["Enter your Item"] ListBox1.Items.Add[myitem] End Sub

For more examples, refer to
//www.vbtutor.net/vb2017/VB2017_Lesson6.html

Video liên quan

Chủ Đề