C# - How to make two forms reference each other

前端 未结 6 1581
忘了有多久
忘了有多久 2020-11-29 12:16

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 tha

相关标签:
6条回答
  • 2020-11-29 12:57

    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;
        }
    }
    }
    
    0 讨论(0)
  • 2020-11-29 12:59

    When you create an instance of Form2 on the frm1 object, this new instance creates another Form1 instance, that creates a new Form 2 instance, that...

    Do you see the infinite loop?

    Create one instance of each other on the main class or as global application variables. Or pass them as parameter to each other. The .NET framework will just reference them without alocating new memory space.

    0 讨论(0)
  • 2020-11-29 13:07

    Have a mediator (Wikipedia Definition) between the two.

    This is an implementation example in C#.

    0 讨论(0)
  • 2020-11-29 13:21

    The "Other form" needs to be given to the appropriate instance. Something like this should help.

    public partial class TogglingForm : Form
    {
        TogglingForm Other {get; set;}
        public TogglingForm()
        {
            InitializeComponent();
        }        
    
        private void HideOther_Click(object sender, EventArgs e)
        {
            Other.Visible = false;
        }
    
        private void ShowOther_Click(object sender, EventArgs e)
        {
            Other.Visible = true;
        }
    }
    
    ....
    
    
    static void Main()
    {
       var first = new TogglingForm();
       var second = new TogglingForm {Other = first};
       first.Other = second;
    
       first.Show();
    }   
    
    0 讨论(0)
  • 2020-11-29 13:21

    You're creating a new instance of each form as you create the other form.

    Instead, you should make one of the forms take an instance its parent form as a parameter.

    0 讨论(0)
  • 2020-11-29 13:22
    namespace WindowsFormsApplication1 
    { 
    public partial class Form2 : Form 
    { 
        Form1 frm1; 
        public Form2(Form1 frm1) 
        { 
            InitializeComponent(); 
            this.frm1 = frm1; 
        } 
    
        private void button1_Click(object sender, EventArgs e) 
        { 
            frm1.Visible = false; 
        } 
    
        private void button2_Click(object sender, EventArgs e) 
        { 
            frm1.Visible = true; 
        } 
    } 
    } 
    
    0 讨论(0)
提交回复
热议问题