Passing Data between UserControls

G

Guest

I have a UserControl containing combo boxes with different values. A second
UserControl needs to be able to retrieve the values from the ComboBoxes in
the first UserControl.

Both controls "live" in the same Windows.Form.

What is the best way to accomplish this? I've expermented with using
properties, static variables etc., none of which I can get to work exactly
like I would like.

One idea that has occurred to me is to use UserControl.FindForm(). This does
indeed seem to point to the right form, since, doing a quickwatch on
FindForm().Name returns the name of the Form both controls live in. However,
Intellisense, doesn't display a reference to the combobox in the first
Control and compilation fails if I manually add the combobox reference.
 
J

Jon Skeet [C# MVP]

<"=?Utf-8?B?QnJpYW4gQ29iYg==?=" <Brian
I have a UserControl containing combo boxes with different values. A second
UserControl needs to be able to retrieve the values from the ComboBoxes in
the first UserControl.

Both controls "live" in the same Windows.Form.

What is the best way to accomplish this? I've expermented with using
properties, static variables etc., none of which I can get to work exactly
like I would like.

One idea that has occurred to me is to use UserControl.FindForm(). This does
indeed seem to point to the right form, since, doing a quickwatch on
FindForm().Name returns the name of the Form both controls live in. However,
Intellisense, doesn't display a reference to the combobox in the first
Control and compilation fails if I manually add the combobox reference.

Why not just pass the second UserControl a reference to the first
UserControl when you create it, or set it as a property?
 
G

Guest

Thanks. That worked for me.

Jon Skeet said:
<"=?Utf-8?B?QnJpYW4gQ29iYg==?=" <Brian


Why not just pass the second UserControl a reference to the first
UserControl when you create it, or set it as a property?
 

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