Datagridview将在后台单击窗体时消失 [英] Datagridview shall disappear when clicking on the Form in the background

查看:168
本文介绍了Datagridview将在后台单击窗体时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所述,我在前面有一个Datagridview表单。 datagridview小于我后面的表单,并且我希望Datagridview在任何地方单击Datagridview时都会消失。

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.

我的代码如下所示:

this.dataGridView1.Leave += new System.EventHandler(this.focus);

,Eventhandler定义如下:

and the Eventhandler is defined like this:

private void focus(object sender, EventArgs e)
{ 
     if(dataGridView1.Focused == false)
     {
         dataGridView1.Visible = false;
     }
}

我的问题是,我的Datagridview只有当一个新的我的表单中的事件被激活,但是当我单击例如在我的表单上的文本框中时不会。

My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.

任何人都可以帮助我?

推荐答案

如果您点击 Form Leave c>或 ToolStripButton PictureBox 或任何其他不可选择的控件。

The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.

如果您期望像下拉列表一样的行为,您可以在 ToolStripControl 主机中托管 DataGridView 并使用 ToolStripDropDown 显示它。这样当您点击DataGridView之外的任何地方时,它将消失。它将像一个下拉菜单。此外,网格可以大于您的格式:

If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControl host and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:

private void button1_Click(object sender, EventArgs e)
{
    this.dataGridView1.Margin = new Padding(0);
    var host = new ToolStripControlHost(this.dataGridView1);
    this.dataGridView1.MinimumSize = new Size(200, 100);
    host.Padding = new Padding(0);
    var dropdown = new ToolStripDropDown();
    dropdown.Padding = new Padding(0);
    dropdown.Items.Add(host);
    dropdown.Show(button1, 0,button1.Height);
}

这是一个例子。最好在现实世界的应用中注意处理物体。

It's an example. It's better to pay attention to disposing of objects in a real world application.

这篇关于Datagridview将在后台单击窗体时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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