我的主要表单中有一些按钮.我希望当我按下任何按钮,打开相同的表格
我的代码(表单加载)
foreach (Control c in this.Controls)
{
button btn = c as Button;
{
Table ss = new Table();
Hide();
ss.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)
但它直接显示我的表单,而不是当我点击我的一个按钮时.
将按钮上的单击事件挂钩到同一个处理程序:
//on your form_load or on the constructor...
foreach (Control c in this.Controls)
{
Button btn = c as Button;
if(c == null)
continue;
c.Click += handle_click;
}
//on your form class
void handle_click(object sender, EventArgs e)
{
Table ss = new Table();
Hide();
ss.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)