如何在 WinForms 中模拟下拉窗口? [英] How to simulate a drop-down window in WinForms?

查看:19
本文介绍了如何在 WinForms 中模拟下拉窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Windows Combobox 控件不过是 ?

但是当我的下拉表单失去焦点时如何隐藏它 - 当它不能失去焦点时?

<小时>

如何在 .NET 中模拟组合框下拉菜单?

<小时>

注意:不要将示例屏幕截图中的内容与其他内容混淆.我在问如何在 Winforms 中创建下拉"表单 - 内容可能与上面的屏幕截图不同:

解决方案

使用 ToolStripControlHostToolStripDropDown 可以达到同样的效果.

来自这个答案:

Private Sub ShowControl(ByVal fromControl As Control, ByVal whichControl As Control)'\ whichControl 需要设置最小尺寸:'whichControl.MinimumSize = whichControl.Size将 toolDrop 调暗为新 ToolStripDropDown()Dim toolHost As New ToolStripControlHost(whichControl)toolHost.Margin = 新填充(0)toolDrop.Padding = 新填充(0)toolDrop.Items.Add(toolHost)toolDrop.Show(Me, New Point(fromControl.Left, fromControl.Bottom))结束子

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...

...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:

解决方案

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

这篇关于如何在 WinForms 中模拟下拉窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆