none
How to limit text in combobox to only items on combobox list

    Question

  • Hi,

     

       I have a combobox with a long list (collection).

    I would like to allow the user to enter text instaed of selecting from the list (since it is so long) but I don't want to allow values there are not on that list.

    I have set AutoCompletion mode to Suggest, but it is still possible to write in items that are not on the list.

     

    Is there any automatic way to do it (I mean without me using the leave event and checking my self)?

     

    Thanks in advance,

    Keren

    Thursday, September 11, 2008 12:22 PM

Answers

  • What do you want that following combination of properties doesn't provide?

     

    AutoCompleteMode = SuggestAppend

    AutoCompleteSource = ListItems

    DrowDownStyle = DropDownList

     

    Other properties left at default.

     

    Friday, September 12, 2008 3:15 AM

All replies

  • Yes, it is called databinding.  Where do the contents of the list come from, a database?  What type of collection are you using to hold the list? 

    It may just be simpler to check it yourself if the data does not come from a database. Post a code example of any input checking that you already have, if any.

    Rudedog
    Thursday, September 11, 2008 1:18 PM
  • Try using suggest and append, that can help although as far as the user is concerned.  What I did was to create a custom class to put into the combobox's item collection.  it gives me the freedom to tie each item in the collection to specific business object but also enables me to put whatever text I want to display in the combobox.

     

    ''' <summary>

    ''' This class allows you to add a text value as well as an object to a combobox item.

    ''' </summary>

    ''' <remarks></remarks>

    Public Class ComboBoxItem

    ''' <summary>

    ''' The text to display in the Drop Down Box.

    ''' </summary>

    ''' <remarks></remarks>

    Public Text As String

    ''' <summary>

    ''' The item that this selection relates to.

    ''' </summary>

    ''' <remarks></remarks>

    Public Tag As Object

    Public Overrides Function ToString() As String

    Return Text

    End Function

    ''' <summary>

    '''

    ''' </summary>

    ''' <param name="text">The text you want to display in the combobox</param>

    ''' <param name="tag">The object you want to tie to this item</param>

    ''' <remarks></remarks>

    Public Sub New(ByVal text As String, ByVal tag As Object)

    Me.Text = text

    Me.Tag = tag

    End Sub

    End Class

     

    Then i do 

    userselection  = nothing

    userselection =  trycast(me.combobox.selecteditem, ComboBoxItem)

    if the cast fails then userselection will be nothing and therefore you know that they have typed something in rather than selecting an item from the list!

     

    Hope this helps

    Thursday, September 11, 2008 4:14 PM
  • A combo-box in dropdown list mode should already handle that for you. If you type something into the combox it will automatically select the closest match.

     

    Thursday, September 11, 2008 4:45 PM
  •  JRQ wrote:
    A combo-box in dropdown list mode should already handle that for you. If you type something into the combox it will automatically select the closest match.

     


    That's correct, but it will still allow you to enter items that are not on the list.  Data binding prevents that.  I wonder what language the OP needs for their solution.
    Thursday, September 11, 2008 5:31 PM
  • Karen,

     

    The following code should do it.  I handled the Leave event, which happens when the control loses focus, and the KeyDown event:

     

    private void comboBox1_Leave(object sender, EventArgs e)

    {

    bool inList = false;

    foreach (string s in comboBox1.Items)

    {

    if (s == comboBox1.Text)

    {

    inList = true;

    break;

    }

    }

    if (!inList)

    {

    comboBox1.Text = "";

    }

    }

     

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)

    {

    if (e.KeyCode == Keys.Enter)

    {

    e.SuppressKeyPress = true; // This will eliminate the beeping

    bool inList = false;

    foreach (string s in comboBox1.Items)

    {

    if (s == comboBox1.Text)

    {

    inList = true;

    break;

    }

    }

    if (!inList)

    {

    comboBox1.Text = "";

    }

    }

    }

     

    I hope this helps.

     

    Bob

    Friday, September 12, 2008 12:54 AM
  • What do you want that following combination of properties doesn't provide?

     

    AutoCompleteMode = SuggestAppend

    AutoCompleteSource = ListItems

    DrowDownStyle = DropDownList

     

    Other properties left at default.

     

    Friday, September 12, 2008 3:15 AM
  • Rudedog2,

     

    Thanks for your reply. Unfortunately it was not helpful.

    It is a List<T> that comes from the databas and gets manipulated before use.

    I do load it with:

     

    List<T> list = .... (get from db)

     

    ... manipulate list

     

    comboBox.DataSource = list;

    And used the DisplayMemeber and ValueMember properties.

     

    But as I mentioned, it is still possible to write whatever.

     

    Keren

    Friday, September 12, 2008 5:36 AM
  • JohnWein,

     

        Works perfectly.

    Just one question: If I want the Text be empty until something is chosen, I must have empty as the first item on my list? There is no other way is there?

     

    Thank for your reply,

    Keren

    Friday, September 12, 2008 5:41 AM
  • To clear the text set the SelectedIndex to -1.

    Friday, September 12, 2008 7:48 AM