Question

I have two from f1 and f2. When f2 is loaded i use this code:

f2 emp = new f2();
emp.MdiParent = f1;
emp.WindowState = FormWindowState.Maximized;
emp.Show();

f1 has a public variable

public string name;

I want to access the f1.name via f2. How can I do that?

Was it helpful?

Solution

I see no f2 in your example. Usually I do something like this:

Create an interface that describes what f1 does:

interface if1
{
    string Name {get;}
}

Implement this in f1:

public class f1 : Form, if1
{
    public string Name { get { return name; } }
}

Pass f1 to f2:

f1 emp = new f1();
f2 x = new f2(f1);

Constructor of f2:

public f2(if1 f1)
{
    string nameOfForm1 = f1.Name;
}

OTHER TIPS

f1 with the Attribute "name":

public class f1 : Form
{
    public string name;
}

f2:

public class f2 : Form
{
    public f2(f1 form)
    {
        string name = form.name;
    }
}

And create them like this:

f1 form = new f1();
f2 emp = new f2(form);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top