Pass Data to Calling Form's Control

G

Guest

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
 
P

Parag Joshi

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
 
G

Guest

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
 
R

RickH

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.
 

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