471,317 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Parent Child Relationships

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
Jan 6 '06 #1
3 1276
"=?Utf-8?B?Um9iS2lubmV5MQ==?=" <Ro********@discussions.microsoft.com>
wrote in news:FB**********************************@microsof t.com:
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
Jan 6 '06 #2
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

"Michael Bray" wrote:
"=?Utf-8?B?Um9iS2lubmV5MQ==?=" <Ro********@discussions.microsoft.com>
wrote in news:FB**********************************@microsof t.com:
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

Jan 6 '06 #3
Parent frm;
frm = (Parent)this.MdiParent;
"Michael Bray" wrote:
"=?Utf-8?B?Um9iS2lubmV5MQ==?=" <Ro********@discussions.microsoft.com>
wrote in news:FB**********************************@microsof t.com:
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

Jan 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by JJ | last post: by
4 posts views Thread by Danny Tuppeny | last post: by
3 posts views Thread by reachsamdurai | last post: by
4 posts views Thread by mikemiller.innova | last post: by
8 posts views Thread by Rick | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.