Calling functions between panels in WinForms C#

G

Guest

I'm working in Visual Studio 2005 and C#.

I have a WinForms application with two panels (left panel and right panel).
I have a textbox on the left panel, and a button on the right panel. I'd like
to have a button-press from the right panel call a function in the left
panel, which might simply Console.Writeline(textBox1.Text);

If I could get the instance of the PanelLeft class, I could simply say:

panelLeftInstance.PrintTextToConsole();

The problem is I don't see how to get an instance of the PanelLeft class. I
placed the PanelLeft and PanelRight panels onto the WinForm from the toolbox.

Thanks,
Randy
 
S

Stoitcho Goutsev \(100\)

randy,

From what I see you have derived your onwn classes from the Panel class and
handle button click event inside this custom panel. In this case you need to
provide the second panel with the reference to the first panel. Other
solution would be the form to expose those panels as public properties and
then inside the second panel you can find the form - FindForm method, cast
it to your form class and use these properties.

However both solution so far make classes tightly coupled. Better would be
the second panel to fire an event when the button is clicked and let the
form call the method on the first panel. This way both panels know squat
about each other and the parent container.
 

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