Passing Variables between Forms
-
i posted before asking how to accomplish this and was replied to, but what they said didn't seem to work (else i was being more thick than usual! :p). this is what i need to do: in an MDI environment, have a variable in FormParent called MyVar. i need to set this within that form (a task i do understand!) and then in my child generated from FormChildTemplate check the variable and set it as something else, then to be able to use it again in FormParent. anyone get the idea and can tell me? thanks in advance, surgeproof. ---------------------------------------------- looking for hosting? ithium is good.
-
i posted before asking how to accomplish this and was replied to, but what they said didn't seem to work (else i was being more thick than usual! :p). this is what i need to do: in an MDI environment, have a variable in FormParent called MyVar. i need to set this within that form (a task i do understand!) and then in my child generated from FormChildTemplate check the variable and set it as something else, then to be able to use it again in FormParent. anyone get the idea and can tell me? thanks in advance, surgeproof. ---------------------------------------------- looking for hosting? ithium is good.
Either pass a reference to the parent form in the constructor (or a property) of the parent form's Type, or cast the child's
MdiParent
to the actual form's Type and access the public or internal property or field. If you just try to access it fromMdiParent
, it won't work because that returns aForm
, which doesn't declare your field or property. That's what you cast it, since your parent form is a derivative ofForm
.Microsoft MVP, Visual C# My Articles
-
Either pass a reference to the parent form in the constructor (or a property) of the parent form's Type, or cast the child's
MdiParent
to the actual form's Type and access the public or internal property or field. If you just try to access it fromMdiParent
, it won't work because that returns aForm
, which doesn't declare your field or property. That's what you cast it, since your parent form is a derivative ofForm
.Microsoft MVP, Visual C# My Articles
thanks. :) you couldn't explain that a little bit more please could you? do you mean when initialising my variable putting something like (FormParent) on the end? or would i try FormParent.MyVar beacuse this doesn't seem to work. thanks. ------------------------------------------------------- looking for hosting? ithium is good.
-
thanks. :) you couldn't explain that a little bit more please could you? do you mean when initialising my variable putting something like (FormParent) on the end? or would i try FormParent.MyVar beacuse this doesn't seem to work. thanks. ------------------------------------------------------- looking for hosting? ithium is good.
It's a simple matter of access. First, your field (not variable, which is a temporary variable declared in a method) or property (properties are recommended to use from other classes) must be either public or internal (for classes within a single assembly), or protected if using a derived class (which you probably wouldn't). Then you must make sure the child form can access that field or property either by casting
MdiParent
or passing a reference to your parent form (FormParent
I'm assuming) to your child form. An example of the former is:public class MyChildForm : Form
{
protected void ShowParentFormsProperty()
{
FormParent form = (FormParent)MdiParent;
if (form != null) MessageBox.Show(form.MyProperty);
}
}The latter way:
public class MyChildForm : Form
{
private FormParent parent;
public MyChildForm(FormParent parent)
{
this.parent = parent; // use "this" when field and parameter are the same.
}
protected void ShowParentFormsProperty()
{
if (parent != null) MessageBox.Show(parent.MyProperty);
}
}In both examples, your
FormParent
may look like this:public class FormParent : Form
{
public FormParent()
{
// If you want to pass the parent in the constructor...
MyChildForm form = new MyChildForm(this);
AddOwnedForm(form);
}
}Microsoft MVP, Visual C# My Articles
-
It's a simple matter of access. First, your field (not variable, which is a temporary variable declared in a method) or property (properties are recommended to use from other classes) must be either public or internal (for classes within a single assembly), or protected if using a derived class (which you probably wouldn't). Then you must make sure the child form can access that field or property either by casting
MdiParent
or passing a reference to your parent form (FormParent
I'm assuming) to your child form. An example of the former is:public class MyChildForm : Form
{
protected void ShowParentFormsProperty()
{
FormParent form = (FormParent)MdiParent;
if (form != null) MessageBox.Show(form.MyProperty);
}
}The latter way:
public class MyChildForm : Form
{
private FormParent parent;
public MyChildForm(FormParent parent)
{
this.parent = parent; // use "this" when field and parameter are the same.
}
protected void ShowParentFormsProperty()
{
if (parent != null) MessageBox.Show(parent.MyProperty);
}
}In both examples, your
FormParent
may look like this:public class FormParent : Form
{
public FormParent()
{
// If you want to pass the parent in the constructor...
MyChildForm form = new MyChildForm(this);
AddOwnedForm(form);
}
}Microsoft MVP, Visual C# My Articles
thanks, i'll try that and say how i got on soon. :) surgeproof ------------------------------------------------------- looking for hosting?ithium is good.