Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
336 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...