0

I am attempting to pass information from a child form to a parent. I have been using the following code I found on a forum to help me:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace childform
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 tempDialog = new Form2(this);
        tempDialog.ShowDialog();
    }

    public void msgme()
    {
        MessageBox.Show("Parent Function Called");
    }

}
}

Form2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace childform
{
public partial class Form2 : Form
{
    private Form1 m_parent;

    public Form2(Form1 frm1)
    {
        InitializeComponent();
        m_parent = frm1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        m_parent.msgme();
    }
}
}

Which works and is all well and good. Trouble is, my program requires me to set variables within tempDialog, from Form 1, in methods other than button1_Click. But, these cannot find the instance of tempDialog because it is in button1_click.

Also, I cannot move it out of a method (say, into the class) because then the 'this' modifier does not reference Form1...

Any ideas how I can reference Form1 from Form2 AND vice versa? Using this code or otherwise?

Thanks

NerdJam
  • 41
  • 1
  • 6

3 Answers3

0

I'm not sure what you mean by your last comment about this. Make a field that references Form2 and initialize it in the constructor. Then you can reference _form2 in other methods of Form1.

public partial class Form1 : Form
{
    private Form2 _form2;
    public Form1()
    {
        InitializeComponent();
        _form2 = new Form2(this);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _form2.ShowDialog();
    }

}

This sort of thing can get difficult to follow, when complexity increases. A better way might be to make Form1 and Form2 have references to some common object that they can both manipulate.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • Thank you, worked perfectly! I don't quite know what you mean about it getting difficult to follow, but I'm sure I'll work it out when I break something! – NerdJam May 20 '13 at 09:32
0

You could try researching about MdiParent and Parent properties on the Form object. These allow you to get and set the parent and access their methods if required.

See Parent property and MdiParent property on MSDN for further information.

Hope this helps.

Rob Aston
  • 816
  • 12
  • 19
0

Depending on your application, if you only ever have one copy of the form that you show and hide in your app, you might just be able to make a singleton static reference. This will only work if you only instantiate the form once however and then use show()/hide() on it to make it dissappear or reappear as required.

public partial class Form2 : Form
{
    public static Form2 Instance;

    public Form2()
    {
        InitializeComponent();
        this.Instance = this;
    }
}

You could then access the form 2 from anywhere by using the code:

Form2.Instance.xxx
Spence
  • 28,526
  • 15
  • 68
  • 103