By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Pass a value from a child form to a Parent

P: n/a
Rob
I have a control (Button) on a Parent form which opens a Windows form... all
I want to do is pass a value from the child form back to the parent... it
should be so simple... i.e.
ParentFormName.Textbox1.text=ChildFormName.TextBox WhatEver.Text... but
nothing like this works.

Any ideas ?
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Rob" <ro***@yahoo.comschrieb
I have a control (Button) on a Parent form which opens a Windows
form... all I want to do is pass a value from the child form back to
the parent... it should be so simple... i.e.
ParentFormName.Textbox1.text=ChildFormName.TextBox WhatEver.Text...
but nothing like this works.

Any ideas ?
General answer: To access an object, you need a reference.

The specific answer depends on many things. First, is the 2nd Form shown
modally (Showdialog)? When do you want to access the other form? Which
event? Writing abstract and reusable code, it's usually not common
practice to access the caller from the callee.
Armin

Jun 27 '08 #2

P: n/a
Rob,

As it is a dialogform, than it is as simple to create a friend property in
your dialog and get that when the dialog is done.

Cor

"Rob" <ro***@yahoo.comschreef in bericht
news:id******************************@comcast.com. ..
>I have a control (Button) on a Parent form which opens a Windows form...
all I want to do is pass a value from the child form back to the parent...
it should be so simple... i.e.
ParentFormName.Textbox1.text=ChildFormName.TextBo xWhatEver.Text... but
nothing like this works.

Any ideas ?


Jun 27 '08 #3

P: n/a
On May 27, 1:34 am, "Rob" <ro...@yahoo.comwrote:
I have a control (Button) on a Parent form which opens a Windows form... all
I want to do is pass a value from the child form back to the parent... it
should be so simple... i.e.
ParentFormName.Textbox1.text=ChildFormName.TextBox WhatEver.Text... but
nothing like this works.

Any ideas ?
My personal method is to have an event on the child form that will
return the value. Then when the child form is created, the parent form
(or any other form that cares about that event) should subscribe to
that event. Then whenever the value is created / changed on the child
form the other forms will be able to act accordingly. This method also
promotes loosely couple forms, which could save some major headaches
in maintenance or reuse.

Thanks,

Seth Rowe [MVP]
Jun 27 '08 #4

P: n/a

A few thoughts:

Form is a class like any other class. You pass values through
properties like you would any other class.

I recommend that you separate your values from the user
interface instead of trying to access them from the controls.

In other words: When the childform class is asked to close,
clientform should grab the values from its controls and store
them in private members, accessible through properties.

One childform has closed, parentform can access the values
through those properties.

For a simple scenario like this, I would suggest that this is the
way to go.

For more complex scenarios where multiple objects need to
react to changes in one or more values, the idea of subscribing
to events has some validity (but do keep track of what object
initiated a change and what objects should just reflect the change).

VB2005 and later supports the VB6 concept of a shared default
instance of a form. I have my reservations about this. I like to
know/control whether a form is freshly initialized or reused.

When/if using ShowDialog to display the form, remember to use
..DialogResult = ... to close it instead of Me.Close.

Regards,

Joergen Bech

On Tue, 27 May 2008 01:34:39 -0400, "Rob" <ro***@yahoo.comwrote:
>I have a control (Button) on a Parent form which opens a Windows form... all
I want to do is pass a value from the child form back to the parent... it
should be so simple... i.e.
ParentFormName.Textbox1.text=ChildFormName.TextBo xWhatEver.Text... but
nothing like this works.

Any ideas ?
Jun 27 '08 #5

P: n/a
On May 27, 7:31 pm, "Rob" <ro...@yahoo.comwrote:
Hi

Regarding... 1) best OO choice - create a message and have the parent
subscribe to
it.

Do you have some sample code that demonstrates this ?
Rather than rewrite someone else's work this article describes it
fairly well.
http://forums.microsoft.com/MSDN/Sho...46657&SiteID=1
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.