passing values, data between diferent forms in c#

teixeira

Regular
Joined
Apr 5, 2005
Messages
94
Location
LEIRIA-PORTUGAL
Hi all,

At some months ago I made this question, but I still have not understand well.
The dought is about passing values, data between diferent forms in c#.
I'm developing an application in a MDI environment, and sometimes i need to pass some values from one forms to others, for example: I have the main form (parent) and for each child form i open, i make visible an item in the menubar. this item alows the user to navigate between openned forms.When i close the child form, i want to set the item (corresponding to this child form) in the parent form as uncheked and invisible.

I searched in some sites and the common way I found its to create a delegate and public function or event and with these we could pass information from one site to other, but all the examples seamed to me not very clare, and i still not able to to do what i need.
Does anyone has an true example to show me or can paste and comment a piece of code here , for me to try understand better this?

Any help will be very welcome,

Best regards,

Teixeira
 
You just need to remember that everything in C# is a pointer. What I usually do to speed things up and keep things simple is feed in the parent form into the child's constructor.

here's what the child's constructor might look like
Code:
frmParent _parent;

frmChild(frmParent parent)
{
   _parent = parent;
}

Here's what a call to the child would then look like:
Code:
frmChild child = new frmChild(this);
child.ShowDialog();
From here, you can reference _parent and set things as needed. To do this, I typically create properties with both a get/set instead of giving public access directly to the parent's form controls -- since you will want to sanitize the incoming information most of the time.

The other things I do a lot of the time is setup an event handler, like you mentioned.
 
If I understand you correctly, this thread might also be a little helpful. Granted, it is in VB, but it would be trivial to translate it to C#.
 
Back
Top