7
votes

I am using combo box in WinForm but when i was selected any item in combo box then selected item background color is blue. i want to remove this blue background color (particularly on form load, tried to set focus to other control in the form, but combo highlight not removed) but item should be selected .

Can anybody help out on this...?

6
Pretty sure you can just set the selected index to -1, but it's been awhile. - asawyer
but item should be selected... - Dhanapal
Are you using a special color theme on windows?? that should not happens on a common combination. Try removing that combo and adding a new one. @asawyer that will clear the combo, but that's not what OP is asking ;) - gbianchi
@Dhana I see, sorry I missed that. I suppose you could always override the rendering and paint the thing yourself. Then it's any color you want. - asawyer

6 Answers

3
votes

It appears that the only way to do this is by subclassing the combobox control.

Here is an example where someone does that:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/e234c4a7-0cf7-4284-a072-8152f7593002/

There are probably more on the web to guide you.

3
votes

To solve the same I have tried almost EVERYTHING:

  • setting the DropdownStyle property to DropdownList
  • this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
  • combobox1.SelectionLength = 0;
  • changing comboBox.TabIndex
  • Not tried SendKeys.Send("{ESC}"); because it is not a reliable solution

Nothing helped. Maybe because I don't have text in my combobox items, only images. The only stable and working solution was to move a focus on another Label control:

    label.Focus();

You could also hide that label.

2
votes

I found something over this site

Create a timer and enable it in your SelectedIndexChanged event, and in the timer just add ComboBox1.Select(0,0) this should remove the selection part and then disable the timer. Identify other entry points where in you can Enable the timer again

Code snippet

private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
    //Get the item selected in the combobox
    ComboBox cbx = (ComboBox)sender;
    int idx = cbx.SelectedIndex;    
    string s1 = cbx.SelectedItem.ToString();
    // Enable the time so that the Highlight can be removed
    timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    // Remove the Highlight
    comboBox1.Select(0, 0);
    // Disable timer
    timer1.Enabled = false;
}
2
votes

I am not a big VB user, and only play with it in Excel, but also had this problem when a selection was made in my ComboBox. I finally found a way to get rid of the blue text highlighting.

I have a ComboBox on a UserForm. By selecting the ComboBox and viewing the Properties, simply changing the 'HideSelection' to True worked for me. You could also code for it: ComboBox1.HideSelection = True

1
votes

There is an Easy solution for you

private void myComboBox_Paint(object sender, PaintEventArgs e)
{
     myComboBoxComboBox.SelectionLength = 0;
}

hope it helps :)

0
votes

I came across the same problem and after not finding a working solution I had the same idea as @Vadim K.

Here is a short example.

First step is to to change focus in the UI_Load event.

Private Sub UI_Load(sender As System.Object, e As System.EventArgs) Handles Me.Load    
    Me.lblTitle.Focus()
End Sub

Next step is to handle the case when someone selects a new value

Private Sub comboExportDates_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboExportDates.SelectedIndexChanged
        Me.lblTitle.Focus()
End Sub

Works fine for me