The Leave
event will not raise if you click on Form
, or a ToolStripButton
, PictureBox
or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView
in a ToolStripControlHost
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);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown
and dispose it when closing the form.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…