简体   繁体   English

如何模拟WinForms中的下拉窗口?

[英]How to simulate a drop-down window in WinForms?

i know a Windows Combobox control is nothing but a Textbox and a ListBox glued together . 我知道Windows Combobox控件只是一个Textbox和一个ListBox粘在一起

i need to simulate the same thing in WinForms. 我需要在WinForms中模拟相同的东西。 i am trying to figure out Windows window options that must be set to achieve the proper effect. 我试图找出必须设置以实现正确效果的Windows窗口选项。

  • 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. 它可以是一个拥有的窗口 - 拥有的窗口总是在z顺序的所有者之上。 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 ) 无边框( this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
  • topmost ( this.TopMost = true ) 最顶层( this.TopMost = true
  • form that doesn't show in the taskbar ( this.ShowInTaskbar = false ) 任务栏中未显示的表单( 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... 这种混乱的集合运作得很好......

在此输入图像描述

...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? 我如何模拟.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: 我问如何在Winforms中创建“下拉”表单 - 内容可能与上面的截图不同:

在此输入图像描述

Using a ToolStripControlHost and a ToolStripDropDown can achieve the same effect. 使用ToolStripControlHostToolStripDropDown可以实现相同的效果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM