Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ComboBox disable highlighting

I have custom ComboBox, where DropDownStyle = ComboBoxStyle.DropDown;.DropDown style is set because I want to set the Text property of the ComboBox to something outside the list of values. Everything works good, except that ComboBox is highlighting the text when it's left and when I click on the combobox editing is avaible. How can I cope with this? To illustrate:

enter image description here

First Picture is where everything looks good, second is the highlight situation, third editing is on.

like image 342
ContentPenalty Avatar asked Sep 17 '14 21:09

ContentPenalty


3 Answers

Try un-selecting the text after the DropDown closes:

void comboBox1_DropDownClosed(object sender, EventArgs e) {
  this.BeginInvoke(new Action(() => { comboBox1.Select(0, 0); }));
}
like image 141
LarsTech Avatar answered Oct 03 '22 03:10

LarsTech


If you are referring to disabling the highlighting and editing, then you might want to consider setting the DropdownStyle property to DropdownList.

yourComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
like image 39
anish Avatar answered Oct 03 '22 05:10

anish


Tricky problem to solve. It seems to be from the Resize event. There are a lot of solutions that do something similar to this, but none that I've seen worked for me until I tried this. (This is a solution that does not require inheritance from ComboBox; inheriting is probably a much more straight forward solution, but requires you to always use your inherited class and never the actual ComboBox class.)

comboBox.Resize += (s, e) => {
   if (!comboBox.IsHandleCreated)
      return;  // avoid possible exception

   comboBox.BeginInvoke(new Action(() => comboBox.SelectionLength = 0));
};

Set the selection length to zero to get rid of the highlight, but when? Other examples do it in other places, but the problem seems to be specifically caused by Resize, so doing it after Resize fixes it consistently, at least for me. (Can still see it flicker when you resize the window though, but it always ends up ok.)

BeginInvoke ensures that it happens sufficiently after Resize to work, and the check for IsHandleCreated prevents it from being called before the handle is created, in which case BeginInvoke would throw an exception.

This slightly more complex version includes some checks to prevent a focused control from losing highlight, since it actually should have it. It also doesn't fire if the parent doesn't exist yet, or if the parent does not have an active control yet, both signs that things are too early.

comboBox.Resize += (s, e) => {
   if (!comboBox.IsHandleCreated)
      return;

   comboBox.BeginInvoke(new Action(() => {
      var parent = comboBox.FindForm();
      if (parent == null)
         return;

      if (parent.ActiveControl == null)
         return;

      if (parent.ActiveControl == comboBox)
         return;

      comboBox.SelectionLength = 0;
   }));
};

I tried to make a version that would 'preserve' the selection length rather than always set it to zero, but I couldn't get it to synchronize properly. Many Resize events can fire before the BeginInvoke delegates start to fire, so the preserved value will always be overwritten by the broken one. I tried saving them all in a Queue or Stack, but in both cases, I was unable to reverse the ordering (not really sure why, since that makes no sense).

like image 21
Dave Cousineau Avatar answered Oct 03 '22 03:10

Dave Cousineau