-1

We Can Access Form2 From Form1 By this way :

In Form1 :

private buttonForm1_click(object sender,EventArgs e)
{
 Form2  frm2 = new Form2()
 frm2.show()
 this.hide()
}

And In Form2 , We want to access Form1 which it has hidden.

In Form2 :

private buttonForm2_click(object sender,EventArgs e)
{
 //What Can I DO ?
 //I Don't Want to Create Another Instance From Form1 

}
Bobby
  • 11,419
  • 5
  • 44
  • 69
S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58
  • possible duplicate of [Access Of Public Method Between Forms](http://stackoverflow.com/questions/4176682/access-of-public-method-between-forms) – H H May 07 '11 at 17:45
  • before asking a question it is a good idea to search for it first. This exact question is asked almost daily here, the sidebar on the left contains a good number of them. – H H May 07 '11 at 18:24
  • -1 because the Related questions contains several good matches. – H H May 07 '11 at 18:25

5 Answers5

1

If you want two forms to have access to each other, they need to know about each other somehow. One way to do this would be a parent/child relationship. However, this probably isn't what you want to do. The other option would be to make the references for each form scoped in such a way so that both have access to them.

A third way to do it would be to pass a reference to Form1 into Form2 when you open Form2. Create a Form1 variable inside of Form2 and populate it when you open Form2 from Form1. That will work when you want two forms to have access to each other but it won't scale well.

IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
1

You can pass a Form1 object to Form2 constructor:

public partial class Form1
{
    // ...

    private buttonForm1_click(object sender,EventArgs e)
    {
       Form2  frm2 = new Form2(this)
       frm2.show()
       this.hide()
    }

    // ...
}

public partial class Form2
{
    private Form1 _form1;

    public Form2(Form1 form1)
    {
        InitializeComponents();

        _form1 = form1;
    }

    // ...

    private buttonForm2_click(object sender,EventArgs e)
    {
        _form1.Show();
    }
}
oxilumin
  • 4,775
  • 2
  • 18
  • 25
  • 1
    If you do that, make sure you also have a parameterless constructor otherwise Visual Studio will not be able to open `Form2` in the design view. – adrianbanks May 07 '11 at 17:37
  • 1
    I would suggest that instead of passing it in via a parameter than you just use the frm2 variable to then populate a variable inside of Form2. For example, frm2.CalingForm = this; That way you can still call Form2 without needing the parameter but you have the option of passing the form reference if you want. – IAmTimCorey May 07 '11 at 17:39
  • @BiggsTRC It depends on what `_form1` is. In some cases null field can break integrity of class and you should pass it throw constructor. – oxilumin May 07 '11 at 17:41
  • @adrianbanks - VS (particularly 2010) normally opens form without parameterless constructor in designer view without any error (even if I add visual components). – oxilumin May 07 '11 at 17:46
1

On Form1:

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            var f = new Form2(this);
            f.Show();
        }
    }
}

On Form2:

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

namespace Form1Form2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        Form1 _f1 = null;
        public Form2(Form1 f) : this()
        {
            _f1 = f;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            _f1.Text = "Nice!";
        }
    }
}
Michael Buen
  • 38,643
  • 9
  • 94
  • 118
0

The easy answer is that in Form2 constructor you pass a reference to Form1.

That way, in Form2 you have a reference you can use and access its controls or call a method in it.

Romias
  • 13,783
  • 7
  • 56
  • 85
0

If you are not creating multiple copies of your forms, I find this to be the easiest method to use. Have the form itself create a handle to it with:

public partial class Form1
{
    public static Form1 Current;

    public Form1()
    {
        InitializeComponents();
        Current = this;
    }
}

public partial class Form2
{
    public static Form2 Current;

    public Form2()
    {
        InitializeComponents();
        Current = this;
    }

    private buttonForm2_click(object sender,EventArgs e)
    {
        Form1.Current.Show();
    }
}

It gets a little more complicated if people close the form. So in the Closing handler do (if you want to keep a handle):

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    Visible = false;
    WindowState = FormWindowState.Minimized;
}

The e.Cancel will keep the form from actually being destroyed, requiring you to create it again. Though if you want it destroyed, you could always create a new one again by converting your Current variable to a Current property that create's a new one in it's get;.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69