jhy*_*yap 3 c# button winforms
表格1中有两个按钮,一个是"ShowForm2"按钮,另一个是"button1"按钮.
默认情况下禁用按钮1.当我点击"ShowForm2"按钮时,表格2将显示.
所以,我想要的是,当我单击表单2中的"button2"时,它将启用表单1中的"button1".
所以,我尝试在我的form2类中这样编码:
public partial class Form2 : Form
{
bool enable_form1_button1;
public bool Enable_form1_button1
{
get { return this.enable_form1_button1; }
set { this.enable_form1_button1 = value; }
}
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
enable_form1_button1 = true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在我的Form1类中,我希望将"enable_form1_button1 = true"传递给表单1并启用表单1 button1.但是怎么做呢?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btb_Showfrm2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
button1.Enabled = frm2.Enable_form1_button1; // I put it here, and it just does not seems right
}
}
Run Code Online (Sandbox Code Playgroud)
那么你可以做的是,将按钮公开为属性,并将当前表单的引用发送到需要控制的表单:
Form1中
public partial class Form1 : Form
{
public Button BtnShowForm2
{
get { return btnShowForm2; }
set { btnShowForm2 = value; }
}
private void btnShowForm2_Click(object sender, EventArgs e)
{
// pass the current form to form2
Form2 form = new Form2(this);
form.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Form2中:
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
private void btnEnabler_Click(object sender, EventArgs e)
{
// access the exposed property here <-- in this case disable it
_form1.BtnShowForm2.Enabled = false;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这就是你要找的东西.
归档时间: |
|
查看次数: |
23111 次 |
最近记录: |