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

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

问题描述

我知道Windows 组合框控件只不过是?


然而,当我失去时,我如何隐藏我的下拉式表单焦点 - 当不能失去焦点






我如何模拟组合框下拉在.NET中下载






注意:不要混淆您在示例截图与其他东西。我正在问如何在Winforms中创建下拉形式 - 内容可能与上面的屏幕截图不同:



解决方案

使用 ToolStripControlHost ToolStripDropDown 可以达到相同的效果。



从< a href =https://stackoverflow.com/a/8611382/719186>此答案:

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

Dim toolDrop作为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


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天全站免登陆