Parent Child Relationships

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, Thank you for reading this post.

Please forgive my ignorance. This may be a simple question but am having
trouble finding a straight answer.

I need pass data from my Child Window back to my parent window. What is the
best way to do this?

In C++ I would do this:

MainDlg * dlgParent; // in child's header
......
ChildWindow dlg; // in MainDlg
dlg.dlgParent = this;
dlg.doModal();
...........
then I could access everything directly:
dlgParent->userName = "USERNAME"; // in Child window
dlgParent->password = "PASSWORD";


I tried using the 'this' pointer and catching a ref to it in C#, but the
compiler doesn't let me do that.

Any ideas? Thank you all.

Rob K
 
I need pass data from my Child Window back to my parent window. What
is the best way to do this?

What I've always done is access parameters on the Child from the Parent.

So for example, you have a child window. It's defined by a class. In that
class, create public accessors to retrieve the values you want, something
like..


public class ChildWindow : System.Windows.Forms
{
...
...
public string Username
{
get { return textBoxUserName.Text; }
}
public string Password
{
get { return textBoxPassword.Text; }
}
}

then when you create this window, you would do something like...

....
ChildWindow cw = new ChildWindow();
string username, password;
if (cw.ShowDialog() == DialogResult.OK)
{
username = cw.Username;
password = cw.Password;
}
....


-mdb
 
Thanks Michael! That makes so much sense!

I was in a mindset for some reason (probably staring at this screen for too
long already today) that it needed to be done the other way.

Thats perfect though. Thank you so much.

Rob K
 
Parent frm;
frm = (Parent)this.MdiParent;


Michael Bray said:
What I've always done is access parameters on the Child from the Parent.

So for example, you have a child window. It's defined by a class. In that
class, create public accessors to retrieve the values you want, something
like..


public class ChildWindow : System.Windows.Forms
{
...
...
public string Username
{
get { return textBoxUserName.Text; }
}
public string Password
{
get { return textBoxPassword.Text; }
}
}

then when you create this window, you would do something like...

....
ChildWindow cw = new ChildWindow();
string username, password;
if (cw.ShowDialog() == DialogResult.OK)
{
username = cw.Username;
password = cw.Password;
}
....


-mdb
 
Back
Top