Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Win Form Panel from User Control

Posted on 2009-05-11
14
Medium Priority
?
676 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:Angel02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 7

Expert Comment

by:zwei
ID: 24357219
Can't you just add the HomePanel to the usercontrol?
0
 

Author Comment

by:Angel02
ID: 24357258
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.
0
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24357800
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.
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24357877
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);
}
0
 

Author Comment

by:Angel02
ID: 24357884
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.
0
 
LVL 7

Expert Comment

by:zwei
ID: 24357886
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.
0
 

Author Comment

by:Angel02
ID: 24357964
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.
0
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 24357989
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.
0
 
LVL 7

Expert Comment

by:zwei
ID: 24358068
Ok, seems complicated enough. I think I'd use a field variable for the panel in that case and give it a public getter.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24358119
...or make the Panel SUBSCRIBE to Custom Events that the UserControl raises...then the UserControl doesn't need a reference to the panel.
0
 
LVL 7

Expert Comment

by:zwei
ID: 24358147
Even better.
0
 

Author Comment

by:Angel02
ID: 24358551
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?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 24359589
(...suprised no one else posted while I was out running errands...)

Here is a simple UserControl with two Events, EraseAll() and EraseControl(), triggered by Buttons:

    public partial class UserControl1 : UserControl
    {

        public UserControl1()
        {
            InitializeComponent();
        }

        public event EraseAllDlg EraseAll;
        public event EraseControlDlg EraseControl;

        public delegate void EraseAllDlg();
        public delegate void EraseControlDlg(UserControl1 uc);
       
        private void button1_Click(object sender, EventArgs e)
        {
            EraseControl(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            EraseAll();
        }

    }

And here is a simple Form that demonstrates them in use:

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                UserControl1 uc = new UserControl1();
                uc.Location = new Point(10 + (i * uc.Size.Width), 10);
                uc.EraseAll += new UserControl1.EraseAllDlg(uc_EraseAll);
                uc.EraseControl += new UserControl1.EraseControlDlg(uc_EraseControl);
                HomePanel.Controls.Add(uc);
            }
        }

        void uc_EraseAll()
        {
            HomePanel.Controls.Clear();
        }

        void uc_EraseControl(UserControl1 uc)
        {
            HomePanel.Controls.Remove(uc);
        }

    }

So the UserControl is either making itself, or all the Controls on HomePanel be removed by raising the events.  Note that the UserControls have NO REFERENCES to the main Form or the parent Panel (HomePanel).
0
 

Author Comment

by:Angel02
ID: 24364683
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 !!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question