简体   繁体   English

C#如何捕获文本框外的鼠标事件

[英]c# how to catch mouse event outside textbox

i need to hide a ListBox when I focus out a textbox. 当我重点放在一个文本框时,我需要隐藏一个列表框。 if i click on a different control or use Tab key then the textbox's "Leave" event occurs. 如果我单击其他控件或使用Tab键,则发生文本框的“离开”事件。 But if I click inside the form, on any free space, then focusout doesn't happen. 但是,如果我在窗体内单击任何可用空间,则不会发生焦点对准。 i saw something called mouse capture but i cant implement it. 我看到了一个叫做“鼠标捕获”的东西,但是我无法实现它。 在此处输入图片说明

i tried this: 我尝试了这个:

private void txtProduct_Enter(object sender, EventArgs e)
{
    listProduct.Show();
    UIElement el = (UIElement)sender;
    el.CaptureMouse();
}  

private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
    if (e.Clicks >= 1)
    {
        txtProduct_Leave(sender, new EventArgs());
    }
}

private void txtProduct_Leave(object sender, EventArgs e)
{
    listProduct.Hide();
}

but obviously it shows error. 但显然它显示错误。 how do i achieve this? 我该如何实现? any help? 有什么帮助吗?

I had to make click event for my groupboxes even if groupbox doesnt have a click event by default. 即使groupbox默认情况下没有click事件,我也必须为groupbox设置click事件。

//my_page.designer.cs  
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);  

//my_page.cs  
private void groupBox2_clicked(object sender, EventArgs e)
{
    listProduct.Hide();
}

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

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