我想在加载第二个无模式窗体(button3
)时禁用主窗体上的按钮(Form2
),然后在关闭无模式窗体时重新启用该按钮。
以下是我尝试的内容:
private void button3_Click(object sender, EventArgs e)
{
Form2 p = new Form2(label1.Text);
p.Show();
if (p.Shown)
this.button3.Click += new System.EventHandler(this.button3_Click);
else
this.button3.Click -= new System.EventHandler(this.button3_Click);
}
答案 0 :(得分:2)
实现此目标的最佳方法是在显示button3
之前停用Form2
,并使用FormClosed
上的Form2
事件重新启用button3
表格关闭后:
public partial class Form1 : Form
{
...
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form and assign the FormClosed event
var form = new Form2(label1.Text);
form.FormClosed += Form2_FormClosed;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
// Occurs when Form2 is closed
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
// Re-enable button3
button3.Enabled = true;
}
}
另一种方法,它将lambda表达式赋给FormClosed
事件:
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form
var form = new Form2(label1.Text);
// Assign a lambda method to the FormClosed event to re-enable button3
form.FormClosed += (s, a) => button3.Enabled = true;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
答案 1 :(得分:0)
我做了类似的事情,我不确定你想要做什么,但也许它会帮助你。
public partial class Form2 : Form
{
private void button3_Click(object sender, EventArgs e)
{
Button3.Enabled = false;
Form2 p = new Form2(label1.Text);
p.ShowDialog();
//the code will stop here until you finish your work on form2
Button3.Enabled=true;
}
它适用于我。
但如果你的form3只是一个短标签使用:
Button3.Enabled= false;
MessageBox.show ("blabla");
Button3.Enabled=true;