How to prevent Combo Box automatic selection of text?

How can I set the combo's text to be the same as the text for one of the combo's Items, but still have the text display UNSELECTED

When I set up the data in a combo box "combo" as follows:

combo.Items.Clear();
combo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
combo.BeginUpdate();
combo.Items.Add( "Item 1" );
combo.Items.Add( "Item 2" );
combo.Items.Add( "Item 3" );
combo.Items.Add( "Item 4" );
combo.Text = "Item X";
combo.EndUpdate();

The combo displays the text "Item X" UNSELECTED and shows none of the items in the drop-down list selected.

If instead of
combo.Text = "Item X";
I say
combo.Text = "Item 2";

The combo displays the text "Item 2" SELECTED and shows the list entry containing "Item 2" as selected in the drop-down list. This is very clever of the combo, but its not what I want.

I want the combo to display the text "Item 2" UNSELECTED as it does for "Item X" above.

So how can I set the combo's text to be the same as the text for one of the combo's Items, but still have the text display UNSELECTED

cheers,
Rod


Answer this question

How to prevent Combo Box automatic selection of text?

  • Boris Mueller

    gqlu wrote:

    I think the work around proposed by Rod may be inconvenient if the user wants to get text from the combobox

    Its a (hopefully temporary) workaround, not a solution.

    gqlu wrote:

    But Why do you want this behaviour

    Selecting a value in a dropdownlist combo does not highlight the text displayed by the combo, but setting the text of a dropdown combo does highlight the text displayed by the combo if that text is the same as one of the elements in the combo list.

    I have both dropdown and dropdownlist combos on a form, all with default values and I want them to look the same - with no highlighting of the text displayed by the combos when the form is first displayed.

    gqlu wrote:

    Do users change the selection of combobox further more

    Yes.
    When a user selects a new value from a combo list the text is highlighted, but the highlight is removed when the combo loses focus.


  • Northwester

    I think the work around proposed by Rod may be inconvenient if the user wants to get text from the combobox.

    But Why do you want this behaviour

    Do users change the selection of combobox further more



  • soni_ace

    you can call it from anywhere but it appears not to work. No idea!

  • fibonacci1123

    One workaround is to add a trailing space to the text, e.g.
    combo.Text = "Item 2 ";
    instead of
    combo.Text = "Item 2";

    any others

  • JavaBoy

    ahmedilyas wrote:
    what happens if you set the SelectedText to String.Empty Or what about the SelectionStart to 0 or even the SelectionLength to 0


    I tried Select(0,0) which should have gotten rid of the selection, but it didn't work either from directly after the assignment to combo.Text or from the combo's selection change handler.

    I think Select(0,0) is probably the answer, but where do I need to call it from



  • Romantic_touch

    Actually, if you set the DropDownStyle property to DropDownList, you can select only valid values from the list. If you set the DropDownStyle property to DropDown, you can type any value in the editable area of the ComboBox.

    so if you really want to highlight the text in a DropDownList style combobox, I'm afraid you should custom that control.



  • Alastair Q

    what happens if you set the SelectedText to String.Empty Or what about the SelectionStart to 0 or even the SelectionLength to 0

  • How to prevent Combo Box automatic selection of text?