[VB] adding an item to a combobox

Nico -  
Hasstag Posted messages 7 Status Member -
Hello,

(I am in Visual Basic),

when I click on a button, I add 2 items to my combobox:

macombo.AddItem (mytext1)
macombo.AddItem (mytext2)

but when I click on this button again, it adds the items in double, e.g.:

mytext1
mytext2
mytext1
mytext2

instead of:

mytext1
mytext2

no matter how many times I click the button. I believe there is something with the index property but...

Thank you in advance.

4 answers

  1. Djeter
     
    hello...

    just do:
    ComboBox1.Clear, before each reload to clear the previous content of the list...

    Good luck !
    1
    1. ramo
       
      I almost have the same problem, but when I type combobox.clear it gives me "'clear' is not a member of System.Windows.Forms.ComboBox"
      I hope to have a response as soon as possible, and thank you in advance.
      0
  2. choubaka Posted messages 5535 Registration date   Status Moderator Last intervention   2 113
     
    hi

    You indeed need to specify the index where you should add the Item. So retrieve the number of Items already in the combobox, normally the first index is equal to zero, so number of Items -1 gives you the last used index.

    If you have 4 Items, the last one is stored in index 3.

    ComboBox1.AddItem("test", ComboBox1.ListIndex);

    It should give something like that, to check

    --
    Chouba,
    Assault Pochard..
    0
  3. skyloo Posted messages 7 Status Member 1
     
    Hello everyone
    I've seen some ways to add a list of choices to the combo box, but every time I type the same thing it doesn't work, for example combo1.additem "mytext" or combo1.additem ("mytext", combo1.listindex)
    What do you think the solution is???
    0
    1. nihilito
       
      maybe the place where you enter the code simply.
      0
  4. Hasstag Posted messages 7 Status Member
     
    Hello,

    I had the same problem, and I wrote this code that checks if the item to be added already exists:

    If Form2.TextBox0.Text <> "" Then
    For i = 0 To ComboBox1.Items.Count - 1
    If ComboBox1.Items(i) = UCase(Form2.TextBox0.Text) Then
    C = 1
    End If
    Next i
    If C = 0 Then
    ComboBox1.Items.Add(UCase(Form2.TextBox0.Text))

    End If
    End If

    I hope this helps you.
    0