Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate a drop-down window in WinForms?

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?

  • SW_SHOWNOACTIVATE?

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

like image 706
Ian Boyd Avatar asked Dec 28 '11 21:12

Ian Boyd


People also ask

How do I create a drop down form in Windows?

Step 2: Drag the ComboBox control from the ToolBox and drop it on the windows form. You are allowed to place a ComboBox control anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the ComboBox control to set the DropDownStyle property of the ComboBox.

Is WinForms obsolete?

Win Form has been used to develop many applications. Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.

What is the replacement for WinForms?

WPF, would be your answer to Windows Forms if you are on the . NET platform.


1 Answers

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
like image 169
LarsTech Avatar answered Oct 15 '22 19:10

LarsTech