Passing data from one form to another when both forms are loaded

J

Jim Heimer

How can I pass data from one child form to another? I have two forms, one
(formX) that holds a datagrid (with the results of a query) and another
(formY) that consists of many textboxes. I'd like to pass, for example, a
row from formX to the textboxes of FormY. In this case, since both child
forms are already loaded (both forms are loaded when the MDI loads), I don't
know how to reference formY from within formX.

Is that possible?
Thanks.
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Based on my understainding, you want to find a way to pass data from one
child form to another child form in MDI application.

To pass data to another form, you should have a reference of that form. For
your applicaiton, I think you should create a public property for parent
form, which stores all the child forms' reference. Then each child form can
refer other child form through the public property.

I write a sample like this:

In ParentForm:

public ArrayList FormList=null;
private void ParentForm_Load(object sender, System.EventArgs e)
{
FormList=new ArrayList();
FormX fx=new FormX();
fx.MdiParent=this;
fx.Show();
FormList.Add(fx);

FormY fy=new FormY();
fy.MdiParent =this;
fy.Show();
FormList.Add(fy);
}

For FormY, because its TextBox is a private variable, you should expose it
through property or method, like this:

public void SetText(string str)
{
this.textBox1.Text =str;
}

Then, in FormX, you can pass data like this:

private void button1_Click(object sender, System.EventArgs e)
{
ParentForm pf=(ParentForm)this.MdiParent;
foreach(object obj in pf.FormList)
{
Form f=(Form)obj;
if(f.Name.Equals("FormY"))
{
FormY fy=f as FormY;
fy.SetText("good");
}
}
}

===================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Have you tried my suggestion? Do you still have any concern?

Please feel free to let me know, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top