Vali Maties

Vali Maties

  • NA
  • 66
  • 2.7k

WinForms Combobox Drop Down on Enter and on Click

Mar 6 2020 5:00 AM
Hi.

I have a custom class based on ComboBox, in Winforms. It is set as DropDownList, as I only want to select an item, not to insert or modify the Text area of it.

I want to make it to drop its list when it has focus, from keyboard, by iterate thru form's items using TAB key, on leave to close its dropped list. All works fine by having this code in Enter and Leave methods (Console lines are only for debugging):

The problem is when I click with mouse on it. The list will drop down, it will close and open again.
The order of events fired on click, if this control does not have focus, is Enter and then MouseClick, Leave event does not fire, so I don't know why DropDownList is closing!

How to make it drop only one time if the focus is not on it and I click with mouse?
 
PS: I cannot use ListBox, because of space on the form. I post an image with my checkbox...
 
  1. private void MyNewComboBoxCheckable_SelectionChangeCommitted(object sender, EventArgs e)  
  2. {  
  3.     SuspendLayout();  
  4.     if (readOnly)  
  5.     {  
  6.         SelectedIndex = prevIndex;  
  7.         Console.WriteLine("SelectionChangeCommited");  
  8.     }  
  9.     ResumeLayout(false);  
  10. }  
  11.   
  12. private void MyNewComboBoxCheckable_Enter(object sender, EventArgs e)  
  13. {  
  14.     prevIndex = SelectedIndex;  
  15.     if (ReadOnly == false)  
  16.     {  
  17.         BorderColor = Color.Red;  
  18.         if (!DroppedDown && Droppable)  
  19.         {  
  20.             DroppedDown = true;  
  21.             Console.WriteLine("Enter - Dropped = set to true, {0}", DroppedDown);  
  22.         }  
  23.     }  
  24.     Invalidate();  
  25. }  
  26.   
  27. private void MyNewComboBoxCheckable_Leave(object sender, EventArgs e)  
  28. {  
  29.     DroppedDown = false;  
  30.     BorderColor = Color.DarkGray;  
  31.     Invalidate();  
  32.     Console.WriteLine("Leave - Dropped = false");  
  33. }  
  34.   
  35. private void MyNewComboBoxCheckable_MouseClick(object sender, MouseEventArgs e)  
  36. {  
  37.     DroppedDown = true;  
  38.     Console.WriteLine("MouseClick");  
  39. }  
  40.   
  41. protected override void WndProc(ref Message m)  
  42. {  
  43.   
  44.     if (ReadOnly)  
  45.     {  
  46.         switch (m.Msg)  
  47.         {  
  48.             case 0x201:  
  49.             case 0x203:  
  50.                 break;  
  51.   
  52.             default:  
  53.                 base.WndProc(ref m);  
  54.                 break;  
  55.         }  
  56.     }  
  57.     else  
  58.     {  
  59.         base.WndProc(ref m);  
  60.     }  
  61.   
  62.     if (m.Msg == WM_PAINT)  
  63.     {  
  64.         using (var g = Graphics.FromHwnd(Handle))  
  65.         {  
  66.             // Uncomment this if you don't want the "highlight border".  
  67.   
  68.             using (var p = new Pen(this.BorderColor, 1))  
  69.             {  
  70.                 g.DrawRectangle(p, 0, 0, Width - 1, Height - 1);  
  71.             }  
  72.         }  
  73.     }  
  74. }  
 

Answers (1)