471,603 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

Calling functions between panels in WinForms C#

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
Feb 16 '06 #1
2 2429
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.
--
HTH
Stoitcho Goutsev (100)

"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:DA**********************************@microsof t.com...
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

Feb 16 '06 #2
Why didn't I think of that? <ggg> That's exactly what I needed. Many thanks.
--
Randy
"Stoitcho Goutsev (100)" wrote:
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.
--
HTH
Stoitcho Goutsev (100)

"randy1200" <ra*******@newsgroups.nospam> wrote in message
news:DA**********************************@microsof t.com...
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


Feb 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.