Link to home
Start Free TrialLog in
Avatar of Angel02
Angel02

asked on

Access Win Form Panel from User Control

I created a win form using C# and a user control. I added a panel on the form called 'HomePanel'. I need to access that panel from the user control to add controls something like

HomePanel.Controls.Add(...);

I accessed the controls in the User Control from the form by adding the UserControl as reference. But how do I access the Panel in the Form FROM the User Control?
Avatar of zwei
zwei
Flag of Sweden image

Can't you just add the HomePanel to the usercontrol?
Avatar of Angel02
Angel02

ASKER

No. I need teh HomePanel on the main Form, because I have several usercontrols (which contain tabcontrols) that have to be added to the panel dynamically, depending on the button I click on the main Form.
So my Homepanel is an empty panel. I am using it just to add or clear the usercontrols (tabcontrols) from the main form everytime.
I'm double checking now, however I'm pretty sure you can access the Parent property of your user control to get a reference to the panel, then you'd just have to cast it to the proper type... again, not 100% sure, but I'm checking that now.
Yup, you can do that. So say you are working in an event handler...
private void somecontrol_Click(object sender, EventArgs e)
{
    Panel ctl = ((Panel)sender).Parent;

    // Add a new control
    Label lbl = new Label();
    lbl.Text = "Hey, I'm new";

    // Add the new control to parent container
    ctl.Controls.Add(lbl);
}
Avatar of Angel02

ASKER

Ya sure. As far as I tried the parent property, I could access the main form from User Control as

this.Parent.Controls.Clear ();
this.Parent.Controls.Add (xyz);

1st one  would clear all the controls on the form. (I need to clear only controls on a particular panel).
2nd one doesn't make sense nyways coz I need to add controls to the Panel only.

Please let me know once you double check.
I mean, can't you add it to both the winform and the usercontrol? That way you can control it from the usercontrol and still have it on the mainform.
Avatar of Angel02

ASKER

This is one option. As I said, I need this panel to add different tabcontrols from several User Controls, adding will work using your option. But I will have trouble clearing the older controls from the panel, as they will be from other unknown user controls.

That is, I need to clear the panel first and then add new controls everytime on the form. Hope I make sense.
It's hard to tell why your code isn't working, but I'm assuming you're trying to call "this.Parent" from the user control. There's two things wrong here:
1) Calling this.Parent gets you an instance of the Form, which tells me that for whatever reason, the call site isn't in the Panel.
2) If that code was being run in your user control, I'm pretty sure it would fail. Because if you're getting a reference to your parent and then telling it to Clear() all it's controls that would include the control that you called the code from. I believe that would cause a runtime exception.
Ok, seems complicated enough. I think I'd use a field variable for the panel in that case and give it a public getter.
Avatar of Mike Tomlinson
...or make the Panel SUBSCRIBE to Custom Events that the UserControl raises...then the UserControl doesn't need a reference to the panel.
Even better.
Avatar of Angel02

ASKER

I thought the custom events would work only to call 'methods' in a form. How does it work with a control like Panel? Can you please give me a code snippet?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Angel02

ASKER

Awesome ! Thanks .... it does give me a very clear idea of using events to achieve the solution ... I didn't realise it was as complicated as this :)

This thread has been of great help.... thank you all !!