Jump to content

help on combobox

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Gz1987

Gz1987

    Newbie

  • Members
  • Pip
  • 9 posts
My question is: How do you prevent the user from entering the same data to the combobox? I know you need a do loop and an if statement. but tried to do that for awhile and only little luck. I will post my code below. The code works sometimes, but i think there must be some logic error within.

    If cboCar.Text <> cboCar.List(cboCar.ListIndex) Then

        

        Do

            cboCar.AddItem cboCar.Text

            ''Add to combo box

            

        Loop Until cboCar.Text <> cboCar.List(cboCar.ListIndex)


    Else


            MsgBox "Already There"

            ''Display Textbox if it's in the combo box already

    End If

Attached Files



#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Your loop is wrong. You are saying:

Index1 = this
Index2 = that

cboCar.Text = that

Loop starts at index1. cbCar.Text does not equal Index1. Loop ends. Loop stops at index1. You see, you are never making it to index2 or beyond. Use a do for or while loop with a counter. Cycle through every index until the end. If no matches were found, add the item.

You can also use a search function, if it returns false then insert the word:
Progressive Search Using a Combobox in VB.NET 2005

#3
Gz1987

Gz1987

    Newbie

  • Members
  • Pip
  • 9 posts
Thank you, i know what i am doing wrong now. but i ran into another trouble spot. for the "For loop" i know i am suppose to loop through the list, but i cannot seem to find the right property for it. is it ".ListIndex", ".List"

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I'm not sure I understand what you are asking. The total number of entries in the combobox would be ".ListCount". The ListIndex is the currently selected item.

Here is a sample for loop:

For i = 0 To cboCar.ListCount Step 1
   if cboCar.Text <> cboCar.List(cboCar.ListIndex) Then
           ' Entry already in list
   End If
Next i


I didn't test or execute this code so there could be errors.

#5
dakrwind123

dakrwind123

    Newbie

  • Members
  • Pip
  • 1 posts
ohh it all makes sense