6

i know a Windows Combobox control is nothing but a Textbox and a ListBoxglued together.

i need to simulate the same thing in WinForms. i am trying to figure out Windows window options that must be set to achieve the proper effect.

  • the drop-down cannot be a child window - otherwise it is clipped to the parent's area
  • conceptually it must be a pop-up window - an overlapped window
  • it can be an owned window - An owned window is always above its owner in the z-order. The system automatically destroys an owned window when its owner is destroyed. An owned window is hidden when its owner is minimized.

The best i've managed so far is to create

  • a borderless (this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None)
  • topmost (this.TopMost = true)
  • form that doesn't show in the taskbar (this.ShowInTaskbar = false)

this borderless topmost form contains my "drop-down" control. i "hide" my dropdown when the dropdown form loses focus:

this.Deactivate += new EventHandler(TheDropDownForm_Deactivate);

void TheDropDownForm_Deactivate(object sender, EventArgs e)
{
   ...

   this.Close();
}

This conglomeration of mess works well enough...

enter image description here

...except that "drop-down" takes focus away from the owner form.

And this is my question, what properties should my popup window have?

But then how do i hide my drop-down form when it loses focus - when it cannot lose focus?


How do i simulate a combo-box drop-down in .NET?


Note: Don't confuse what you see in the example screenshot with something else. i am asking how to create "drop-down" form in Winforms - the contents can be different than the screenshot above:

enter image description here

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • possible duplicate of [How to create a C# Winforms Control that hovers](http://stackoverflow.com/questions/353561/how-to-create-a-c-sharp-winforms-control-that-hovers) – Hans Passant Dec 28 '11 at 21:57
  • I am curious as to where / how / why you would need something like this? You may want to consider using a WPF control (much easier to manipulate and you can use with WinForms). – tsells Dec 28 '11 at 22:52
  • @HansPassant Fundamentally that answer can't serve me, because the control is "parented". And if parented then it will clip to it's parent (i.e. the "drop-down" can exist outside of its "parent" form) – Ian Boyd Dec 29 '11 at 00:07
  • you can find another solution here: [stackoverflow.com/a/15305176/1522062](http://stackoverflow.com/a/15305176/1522062) – Zoltan Tirinda May 05 '13 at 09:53

1 Answers1

4

Using a ToolStripControlHost and a ToolStripDropDown can achieve the same effect.

From this answer:

Private Sub ShowControl(ByVal fromControl As Control, ByVal whichControl As Control)
  '\\ whichControl needs MinimumSize set:'
  whichControl.MinimumSize = whichControl.Size

  Dim toolDrop As New ToolStripDropDown()
  Dim toolHost As New ToolStripControlHost(whichControl)
  toolHost.Margin = New Padding(0)
  toolDrop.Padding = New Padding(0)
  toolDrop.Items.Add(toolHost)
  toolDrop.Show(Me, New Point(fromControl.Left, fromControl.Bottom))
End Sub
Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Seems reasonable, except for `toolDrop.Show` throws *Top-level control cannot be added to a control.*. Presumably because i am trying to show a `Form`. – Ian Boyd Dec 29 '11 at 19:49
  • @IanBoyd True, but then just make `TopLevel = false;` – LarsTech Dec 29 '11 at 19:52
  • Excellent with the `TopLevel`, i had no idea that existed (or how it differs from `TopMost`). Bonus: it even has the "popup shadow". The only thing left is how do i know when the "popup" is closed? Neither the `Deactivate` or `FormClosed` event fire. – Ian Boyd Dec 29 '11 at 20:03
  • @IanBoyd Look at the ToolStripDropDown events. You will have `Opened`, `Opening`, `Closed` and `Closing` events you can wire up. – LarsTech Dec 29 '11 at 20:12
  • Thanks, Lars - i think there's enough information here to solve the drop-down answer. It's going to take a bit of work so i can sort out the jumbled hodge-podge mess to the point where's a good enough-generic answer for the other generic "popup" question. Right now all the code intertwined. – Ian Boyd Dec 29 '11 at 20:25