Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

winforms - C# - How to make two forms reference each other

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;
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...