C# - 如何使两种形式的相互引用 [英] C# - How to make two forms reference each other
问题描述
我写在微软的Visual C#中WindowsForms应用程序,我需要两种形式可以相互引用。正如我测试,我创建Form1上的两个按钮 - 显示窗体2和另一个按钮,隐藏它(code是下文)
一键我想做同样的事情窗体2 - 创建一个隐藏或显示Form1的两个按钮。我用我用Form1的同样的方法,但是当我编译应用程序,它似乎会陷入一个死循环,我也得到一个计算器消息。
我怎样才能改变code,使两种形式都能够相互引用?
Form1中code:
命名空间WindowsFormsApplication1
{
公共部分Form1类:表格
{
窗体2 FRM2;
公共Form1中()
{
的InitializeComponent();
FRM2 =新Form2的();
} 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
frm2.Visible = FALSE;
} 私人无效button2_Click(对象发件人,EventArgs的发送)
{
frm2.Visible = TRUE;
}
}
}
窗体2 code:
命名空间WindowsFormsApplication1
{
公共部分类窗体2:表
{
Form1中FRM1;
公共窗体2()
{
的InitializeComponent();
FRM1 =新Form1的();
} 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
frm1.Visible = FALSE;
} 私人无效button2_Click(对象发件人,EventArgs的发送)
{
frm1.Visible = TRUE;
}
}
}
Forms2的code应
命名空间WindowsFormsApplication1
{
公共部分类窗体2:表
{
Form1中FRM1;
公共窗体2(Form1的母公司)
{
的InitializeComponent();
FRM1 =父母;
} 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
frm1.Visible = FALSE;
} 私人无效button2_Click(对象发件人,EventArgs的发送)
{
frm1.Visible = TRUE;
}
}
}
尽管两者彼此交谈,人们必须创建第一和传递到第二个
Form1中需要将tweeked到
公共Form1中()
{
的InitializeComponent();
FRM2 =新的窗体2(本);
}
另一种方式来做到这一点是同时创建和建设后,将它传递
命名空间WindowsFormsApplication1
{
公共类SomewhereElse
{
公共无效SomeFunction()
{
Form1上Form1中=新Form1的();
Form2的窗口2 =新Form2的(); form1.frm2 = Form2的;
form2.frm1 = form1的;
}
}公共部分类窗体2:表
{
公共Form1中FRM1 {搞定;组;}
公共窗体2(Form1的母公司)
{
的InitializeComponent();
} 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
frm1.Visible = FALSE;
} 私人无效button2_Click(对象发件人,EventArgs的发送)
{
frm1.Visible = TRUE;
}
}公共部分Form1类:表格
{
公共窗体2 FRM2 {搞定;组;}
公共Form1中()
{
的InitializeComponent();
} 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
frm2.Visible = FALSE;
} 私人无效button2_Click(对象发件人,EventArgs的发送)
{
frm2.Visible = TRUE;
}
}
}
I'm writing a WindowsForms application in MS Visual C#, and I need two forms to be able to reference each other. As I test, I created two buttons on Form1 -- one button that displays Form2 and another button that hides it (code is below).
I want to do the same thing for Form2 -- create two buttons that hide or show Form1. I used the same method that I used for Form1, but when I compile the application, it seems to get caught in an infinite loop and I get a StackOverflow message.
How can I change the code so that both forms are able to reference each other?
Form1 code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
frm2 = new Form2();
}
private void button1_Click(object sender, EventArgs e)
{
frm2.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
frm2.Visible = true;
}
}
}
Form2 code:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 frm1;
public Form2()
{
InitializeComponent();
frm1 = new Form1();
}
private void button1_Click(object sender, EventArgs e)
{
frm1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
frm1.Visible = true;
}
}
}
Forms2's code should be
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Form1 frm1;
public Form2(Form1 parent)
{
InitializeComponent();
frm1 = parent;
}
private void button1_Click(object sender, EventArgs e)
{
frm1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
frm1.Visible = true;
}
}
}
Even though the both talk to each other, one must be created first and passed to the second one.
Form1 will need to be tweeked to
public Form1()
{
InitializeComponent();
frm2 = new Form2(this);
}
The other way to do it is create both and pass it after construction
namespace WindowsFormsApplication1
{
public class SomewhereElse
{
public void SomeFunction()
{
Form1 form1= new Form1();
Form2 form2= new Form2();
form1.frm2 = form2;
form2.frm1 = form1;
}
}
public partial class Form2 : Form
{
public Form1 frm1 {get; set;}
public Form2(Form1 parent)
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frm1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
frm1.Visible = true;
}
}
public partial class Form1 : Form
{
public Form2 frm2 {get; set;}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frm2.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
frm2.Visible = true;
}
}
}
这篇关于C# - 如何使两种形式的相互引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!