本文介绍了组合框如何显示列表项时,将鼠标事件拦截到表单上的任意位置以隐藏列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用类似于组合框的功能实现.net表单控件,但是我不知道在表单上的任何位置拦截鼠标事件的正确方法来展开项目列表。 p>

如何在列表显示时如何防止其他控件响应鼠标事件?



如何有效地并安全地将鼠标点击事件抓到表单上的任何地方,以隐藏扩展列表。

解决方案

只需使用 ToolStripControlHost 以及 ToolStripDropDown ,它将像 ComboBox 落下。您不必担心处理鼠标事件。



我以前使用过:

  Private Sub ShowControl(ByVal fromControl As Control,ByVal whichControl As Control)
'\\ whichControl需要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

使用表单上的按钮控件快速演示:

 code> Private Sub Button1_Click(ByVal sender As Object,ByVal e As EventArgs)Handles Button1.Click 
ShowControl(Button1,New MonthCalendar)
End Sub

要回答标题中的问题,我想 p调用 SetCapture 发布Capture 用于处理这种类型的功能。


I'm trying to implement a .net form control with functionality similar to a combo box, but I don't know the proper method to intercept mouse events anywhere on the form to un-expand the list of items.

How do I prevent other controls from responding to mouse events while the list is being shown?

How do I efficiently and safely catch a mouse click event to anywhere on the form, to hide the expanded list?

解决方案

Just use a ToolStripControlHost along with the ToolStripDropDown, and it will work just like the ComboBox dropdown. You won't have to worry about handling the mouse events.

I've used this before:

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

Quick Demo with a Button control on a form:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
  ShowControl(Button1, New MonthCalendar)
End Sub

To answer the question in your title, I think the pinvoke calls of SetCapture and Release Capture are used to handle that type of functionality.

这篇关于组合框如何显示列表项时,将鼠标事件拦截到表单上的任意位置以隐藏列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:48