How to access Panel Control

H

Harry

I have a simple C# windows app with 3 forms(frmMain, frmTest1 and
frmTest2). The frmMain form has a toolbar control docked to top,
groupBox control docked to the left and a panel control dock style
with Fill. frmTest1 have lots of textboxes, buttons, etc. controls on
it. frmTest2 is same as frmTest1 with lots of controls. When I click a
button on the toolbar or groupbox control, I load frmTest1 controls
into the panel control in frmMain. When I click on another toolbar
button, I can clear the panel control and load the other form controls
into panel control. What I would like to do is, changing the panel
from one of the current panel control. Let's say I've loaded the
frmTest1 form into panel. And I would like change the panel after a
button event fired within the panel and change to frmTest form
controls. I can do that from the toolbar buttons, but I cannot do it
within the panel.
Here is how I load the frmTest1 in to panel control in frmMain;

private void button1_Click(object sender, System.EventArgs e)
{
panel.Controls.Clear();
Form frmTest1 = new frmTest1();
frmTest1.TopLevel = false;
panel.Controls.AddRange(new System.Windows.Forms.Control[] {
frmTest1 });
frmTest1.Show();
}

How do I make this call from the frmTest1 buttonTest click event to
load the frmTest2 form controls into panel? Should I make the call
from frmMain or in the frmTest1? I've tried from both but, couldn't
make it work.

Any help will be appriciated.

Thank you in advance.
 
Y

Ying-Shen Yu[MSFT]

Hi Harry,

From my understanding now, you want to show another set of controls on the
panel after clicking a button in the current panel.

Could you show me the code snippet of the frmTest1.buttonTest click event
handler (which you tried to add the new form)?

Also You may try this code snippet
<code>
private void button1_Click(object sender, System.EventArgs e)
{
frmTest2 = new frmTest2();
frmTest2.TopLevel = false;
frmTest2.Parent = this.Parent;//setting parent will add to
the Parent.ControlsCollection automatically
this.Dispose();//unload curren form
}
</code>

Also, in this case, you may try using UserControl instead of the form,
since it's designed to be used as a customizable container control.

Please be free to reply to this thread if you still have problem on it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
G

Guest

Thank you Ying-Shen,
Instead of using ' panel.Controls.AddRange(new Forms.Control[] { frmTest1 }); '
after your reply, I used ' frmTest2.Parent = this.Parent;'. And this solved the problem.
Now I'm challenging another problem.
When I click on any control(from frmTest1 etc.) in panel(in frmMain), how do I update the groupBox controls in the frmMain?
Thanks a lot.

Harry


----- \"Ying-Shen Yu[MSFT]\" wrote: -----

Hi Harry,

From my understanding now, you want to show another set of controls on the
panel after clicking a button in the current panel.

Could you show me the code snippet of the frmTest1.buttonTest click event
handler (which you tried to add the new form)?

Also You may try this code snippet
<code>
private void button1_Click(object sender, System.EventArgs e)
{
frmTest2 = new frmTest2();
frmTest2.TopLevel = false;
frmTest2.Parent = this.Parent;//setting parent will add to
the Parent.ControlsCollection automatically
this.Dispose();//unload curren form
}
</code>

Also, in this case, you may try using UserControl instead of the form,
since it's designed to be used as a customizable container control.

Please be free to reply to this thread if you still have problem on it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
Y

Ying-Shen Yu[MSFT]

Hi Harry,

Thanks for your reply.

You can get the mainform object by retrieving the Parent property of the
control repeatedly,
e.g. Me.Parent.Parent,(the first Parent should be the panel, the second
Parent should be the parent of the panel). However it would be more clear
to define a reference of your mainform type in the frmTest2 class and then
pass in the reference of your mainform when you create the frmTest2
object.

Does this way solve your problem?
If you have any problem on it, be free to reply to our newsgroup!
Thanks!


Happy Holidays!

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
H

Harry

Thanks for your reply Ying-Shen,

Can you give me an example code based on my application scenario?

Thank you very much in advance

Harry
 
Y

Ying-Shen Yu[MSFT]

Hi Harry,

Sure,
From my understanding now, your panel was placed on frmMain, and your
frmTest is placed in the panel, also your group box control is on the
frmMain.

In this senario in frmTest class, you may get the reference of the frmMain
by
frmMain form = this.Parent.Parent as frmMain;

Does this solve your problem?
If you still have problem on it, please be free to reply this thread to let
me know!
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
H

Harry

Thanks Ying-Shen,

That's worked very well. I had to change the groupbox control modifier to
public
to access it from frmTest.
Do you know if there is any other way of doing this without making a control
public?
If there is not, it's just fine.
Thanks a lot for your great help.

Harry
 
Y

Ying-Shen Yu[MSFT]

Hi Harry,

You may define a public method to return the groupbox in your frmMain class.
Or you may also pass the reference of frmMain to your frmTest , when
creating the frmTest object. here is a simple code snippet:
<code>
class frmTest2: Form
{
private frmMain _form;
public frmTest2(frmMain form)
{
_form = form;
}
}

//assume you create frmTest2 object in frmTest1 class when createing the
frmTest object
frmTest test = new frmTest2((frmMain)this.Parent)
</code>

Does this solve your problem?
Plese be free to reply this thread to let us know, if you have any further
questions on it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

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