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

Pass Data to Calling Form's Control

P: n/a
I have a form, Form1, that shows an instance of Form2 when a user clicks a
button. On Form2, the user is to select items, Customer Names for example,
from a datagrid control. When the user clicks a button on Form2, I want to
pass the text of the selected Customer Names in the datagrid back to a
listbox control on Form1, but I'm stuck as to how to access the listbox back
on Form1. I have no problem getting the information I want from the datagrid,
I'm just stuck as to how to pass it back to the instance of Form1 that called
Form2 in the first place.

Any help is greatly appreciated.

Thanks,
Rich
Apr 12 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
HI Rich
One way of doing it is to declare an instance of Form2 in Form1 and then
show that form on the button click. Since its a local member variable, you
will have access to the data.
What you can do is declare an event in Form2. When the user clicks the
button on Form2, raise the event.

Trap the event in Form1 and then read its value. Its even easier if you
declare an object to hold the data you need and pass that object by ref to
Form2.
Hope this helps
Parag

"Rich Hutchins" <Ri**********@discussions.microsoft.comwrote in message
news:C7**********************************@microsof t.com...
>I have a form, Form1, that shows an instance of Form2 when a user clicks a
button. On Form2, the user is to select items, Customer Names for example,
from a datagrid control. When the user clicks a button on Form2, I want to
pass the text of the selected Customer Names in the datagrid back to a
listbox control on Form1, but I'm stuck as to how to access the listbox
back
on Form1. I have no problem getting the information I want from the
datagrid,
I'm just stuck as to how to pass it back to the instance of Form1 that
called
Form2 in the first place.

Any help is greatly appreciated.

Thanks,
Rich

Apr 12 '07 #2

P: n/a
Parag,

I really appreciate your prompt reply. I think I follow what you're saying.
I'm also rather embarrassed to say that after looking around for the answer
for a couple hours then posting my question, I found what looks to be a
reasonable solution (it's also very close to what you've posted).

Basically, it's in the post entitled "Getting a return value from a form"
and the post is from Earl. He explains that when you call Form2 from Form1,
Form2 and its properties, variables, etc. do not go out of scope until Form1
disposes of Form2 even if Form2 is no longer visible. I played with that a
little with my own form and it seems to work the way I need it to work.
You've pretty much offered the same advice and given me another way to change
Form1 using an event on Form2. Being new to VB .NET, I did not completely
understand the lifecycle of Form2 in this context. I just assumed
(incorrectly) that once it was gone, everything associated with it was gone
as well.

Thanks again, Parag, and I'm sorry if I've wasted your time.

Rich
Apr 12 '07 #3

P: n/a
On Apr 12, 3:10 pm, Rich Hutchins
<RichHutch...@discussions.microsoft.comwrote:
I have a form, Form1, that shows an instance of Form2 when a user clicks a
button. On Form2, the user is to select items, Customer Names for example,
from a datagrid control. When the user clicks a button on Form2, I want to
pass the text of the selected Customer Names in the datagrid back to a
listbox control on Form1, but I'm stuck as to how to access the listbox back
on Form1. I have no problem getting the information I want from the datagrid,
I'm just stuck as to how to pass it back to the instance of Form1 that called
Form2 in the first place.

Any help is greatly appreciated.

Thanks,
Rich
Launch Form2 as a modal dialog, in form2 take the users selection an
put it into DialogResult property, then in Form1 inspect the value of
Form2's DialogResult property. Make sure you destroy Form2 after
Form1 gets what needs from DialogResult, because it will live on in
memory.
Apr 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.