locked
Checking if combobox contains specific text

    Question

  • Hi,

    How do I write code to check if combobox contains a particular text?

    Please provide code

     

    Saturday, June 09, 2007 7:08 AM

Answers

  • Hi there,

    Is the purpose of this to:

    (1) Find if the text in the editable part of the combo box contains what you're looking for; or
    (2) Find if there is an item in the combo box (not necessarily the selected one) which contains what you're looking for

    If it's (1) then you could do something like:



    Code Snippet
    If ComboBox1.Text.Contains("<Insert Your Text Here>") Then
    'Process as required
    End If

    If it's (2) then you could do something like:

    Code Snippet
    For Each cboItem As Object In ComboBox1.Items
    If cboItem.ToString().Contains("<Insert Your Text Here>") Then 'Assuming strings are the combo box item so ToString() gives text
    'Process As Required
    End If
    Next

    The above are simplistic examples just to illustrate how you could do it. Note that I haven't taken into account case-sensitivity and such because I'm not sure what your exact circumstances are....But the examples above are case sensitive.

    If you don't need case sensitivity you could do something like call ToLower() on the strings you want to compare and then use Contains(). There are other ways to do this as well (case insensitivity) which are a lot better than my suggestion and they should be easy to find if you do a Google search or similar

    Hope that helps a bit
    Saturday, June 09, 2007 7:36 AM
  • you're question can be read two ways, previous posted addressed scanning the list and finding text.  i read your question as "how do i check if the combobox is showing particular text (ie. get the value of the currently selected item)

     

    If youre using "DropDown" for your DropDownStyle, you can just use ComboBox1.SelectedText

     

    I your DropDownStyle is "DropDownList", its ComboBox1.SelectedItem (which is actually an object, not a string, but it gets the job done)

     

    Saturday, June 09, 2007 8:23 PM

All replies

  • Hi there,

    Is the purpose of this to:

    (1) Find if the text in the editable part of the combo box contains what you're looking for; or
    (2) Find if there is an item in the combo box (not necessarily the selected one) which contains what you're looking for

    If it's (1) then you could do something like:



    Code Snippet
    If ComboBox1.Text.Contains("<Insert Your Text Here>") Then
    'Process as required
    End If

    If it's (2) then you could do something like:

    Code Snippet
    For Each cboItem As Object In ComboBox1.Items
    If cboItem.ToString().Contains("<Insert Your Text Here>") Then 'Assuming strings are the combo box item so ToString() gives text
    'Process As Required
    End If
    Next

    The above are simplistic examples just to illustrate how you could do it. Note that I haven't taken into account case-sensitivity and such because I'm not sure what your exact circumstances are....But the examples above are case sensitive.

    If you don't need case sensitivity you could do something like call ToLower() on the strings you want to compare and then use Contains(). There are other ways to do this as well (case insensitivity) which are a lot better than my suggestion and they should be easy to find if you do a Google search or similar

    Hope that helps a bit
    Saturday, June 09, 2007 7:36 AM
  • you're question can be read two ways, previous posted addressed scanning the list and finding text.  i read your question as "how do i check if the combobox is showing particular text (ie. get the value of the currently selected item)

     

    If youre using "DropDown" for your DropDownStyle, you can just use ComboBox1.SelectedText

     

    I your DropDownStyle is "DropDownList", its ComboBox1.SelectedItem (which is actually an object, not a string, but it gets the job done)

     

    Saturday, June 09, 2007 8:23 PM