Multiple Forms - passing calls to and from
I'm working on a project that has two separate forms--one main form, and one secondary form. Both forms have controls and functions that the other forms needs to be able to read to and from. I'm struggling with how best to accomplish this. The way I've got it set up now:
Main form: frmMain
Code:
namespace nsSpace
{
public partial class frmMain : Form
{
frmSecondary frmTwo = new frmSecondary();
public frmMain()
{
InitializeComponent();
frmTwo.Show(); //Open secondary Form
frmTwo.comboBox1.Items.Add("Item 1");
frmTwo.callSomeFunction();
}
}
}
Secondary form: frmSecondary
Code:
namespace nsSpace
{
public partial class frmSecondary : Form
{
public frmSecondary()
{
InitializeComponent();
frmMain.callAnotherFunction();
frmMain.comboBox5.Items.Add("Item 3");
}
}
}
The above code will open both forms, and frmMain seems to be able to make calls to frmSecondary just fine. The problem is making calls to frmMain FROM frmSecondary. Doing so produces the error: "An object reference is required for the nonstatic field, method, or property nsSpace.frmMain.callAnotherFunction()"
I've tried declaring some things as static, but it just seems to make more things go wrong. Any help would be awesome.
Re: Multiple Forms - passing calls to and from
You can't just refer to frmMain and expect to get access to your main form. frmMain is a class. You could create hundreds or thousands of instances of that class if you wanted. Which one of those would frmMain refer to? The answer is none of them.
Just like your main form has a variable of type frmSecondary to refer to the frmSecondary object, so too the secondary form needs a variable of type frmMain to refer to your frmMain object. Where will it get this reference top the main form? When a child is born it knows nothing about its parents. The parents have to tell the child "we are your parents".
So too your secondary form knows nothing about the frmMain object that created it. The main form has to pass it a reference to itself so the secondary form is able to refer back to the main form. The secondary form needs a method or a property that accepts a frmMain object that it can then assign to a member variable. It can then refer to the frmMain object via that variable.
Finally, can I suggest that you do not use Hungarian notation on type names? I suggest not using Hungarian at all but it was NEVER intended to be used on types, only variables. The idea is that the prefix tells you the type of the variable. Do you see anywhere in the .NET Framework names with prefixes like that? Your types should have names that describe their purpose, like MainWindow, SaveDialogue, etc. Namespaces too. Why do you need "ns" at the beginning of a namespace name? None of the multitude of namespaces in the Framework have that. Will you ever forget that they are namespaces?
Re: Multiple Forms - passing calls to and from
I think you've come from a VB6 background. VB6 lets you treat forms in some special ways, although you can treat them just as any other class (and indeed, this is recommended). In .NET those "special ways" no longer exist.
I suggest you follow some tutorial on object-oriented programming (OOP) concepts. A good understanding of OOP is crucial to avoiding any confusion when using .NET.
Re: Multiple Forms - passing calls to and from
I had tried creating an instance of the main form class in the secondary form, but it seemed to me that I had created an infinite loop of forms.
Code:
namespace nsSpace
{
public partial class frmSecondary : Form
{
frmMain MainForm = new frmTest();
public frmSecondary()
{
InitializeComponent();
MainForm.callAnotherFunction();
}
}
}
In what way can I have the main form pass a reference to itself to the secondary form?
And, yeah, I come from a VB6 background so hungarian notation and playing around with forms is baggage from those days. Thanks for the tips however.
Re: Multiple Forms - passing calls to and from
I figured out a way to make this work. It may not be the most elegant, but it's certainly better than the infinite loop or declaring everything in sight as static. I created a new class called "ShareForm" in which I give a reference to Form1.
ShareForm.cs:
Code:
namespace TestForm2
{
class ShareForm
{
public static Form1 FormOne;
}
}
Form1 opens Form2 just like it always had by created a new Form2 instance. But now, in the form_load event I pass the current instance of Form1 to the variable in the ShareForm class:
Code:
namespace TestForm2
{
public partial class Form1 : Form
{
Form2 FormTwo = new Form2();
public Form1()
{
InitializeComponent();
FormTwo.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
ShareForm.FormOne = this;
}
public void FillComboBox()
{
FormTwo.comboBox1.Items.Add("Item One");
}
private void button1_Click(object sender, EventArgs e)
{
FillComboBox();
}
}
}
Form2 refers to the shared FormOne variable:
Code:
namespace TestForm2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ShareForm.FormOne.FillComboBox();
}
}
}
This currently suits my needs. If you folks have a better solution, I'm all ears (eyes?). Thanks for the input.
Re: Multiple Forms - passing calls to and from
You don't create an instance of the main form in your second form. You already have an instance, so if you create another then you have two. If you make changes to one then the other is unaffected.
You should also not be using any static variables for this. That is an abuse of the static key word. I've already told you how to do it. You declare a variable in the second form of the type of the main form then, when the main form creates the second form it passes a reference to itself. Voila! The second form now has a reference to the correct instance of the main form. In the second form it looks like this:
C# Code:
private frmMain _mainForm;
public frmMain MainForm
{
set
{
this._mainForm = value;
}
}
In the main form it looks like this:
C# Code:
frmSecondary secondForm = new frmSecondary();
secondForm.MainForm = this;
secondForm.Show();
Now the second form has a reference to the main form in its _mainForm variable.