views:

25

answers:

2

I really had no idea what to title this question.

Assume I have a windows form application. The GUI is complex enough to require two custom user controls, "LeftSide" and "Rightside" which each are composed from various buttons, labels, and maybe even another custom user control.

My question:

I am in in the scope of the "Rightside" control. How would I call a method from the "Leftside" control?

I am using Visual Studio 2008.

+1  A: 

The simplest solution is to make a property on the RightSide control of type LeftSide, then set it to the LeftSide instance in the form designer.

You can then call public methods on the property.

However, this is poor design.
Each usercontrol should be a self-contained block that doesn't need to directly interact with other usercontrols.

You should consider restructuring your form.

SLaks
Followup Question: Would I be able to move the method in question from "Leftside" to the parent form itself, such that both "Leftside" and "Rightside" could call the method?
Raven Dreamer
@Raven: Yes. Make it a public method, then write `((MyParentFormClass)FindForm()).SomeMethod()`
SLaks
+1  A: 

Exact equivalent with standard WF controls: how to keep the text of one text box in sync with another:

    private void textBox1_TextChanged(object sender, EventArgs e) {
        textBox2.Text = textBox1.Text;
    }

Necessary ingredients: an event on your user control that is fired when something interesting happens. And public properties.

Hans Passant