troubleshooting Question

User Control Constructors.

Avatar of San24
San24 asked on
.NET ProgrammingC#Editors IDEs
12 Comments10 Solutions523 ViewsLast Modified:
Experts,

I`m using C# VS2008 3.5. Windows forms.

I was wondering if I can pass a UserControl to the Constructor of another UserControl. Is that even possible?

This is what I want to achieve -  I need to switch betwween / toggle between two User Controls. I have this working by making them properties of each other and handling the visibility of the UserControls.

I wanted to take a different approach.

Here is what I`m trying to do - I want to load User Control A (UA) on the main form. and then toggle between UA and UB when I click on their buttons.

I was hoping there could be a more elegant way to do it, rather than change their visibility.

Event In Main Form
  private void But_Click(object sender, EventArgs e)
        {
            UA Ax;
            UB Bx;
         
            Ax = new UA(Bx);   //This Won`t wort, But I was hoping I can do it a different way.
            Bx = new UB(Ax);

            panel1.Controls.Clear();
            panel1.Controls.Add(Ax);
        }

//UA User Control
   public partial class UA : UserControl
    {
        public UA()
        {
            InitializeComponent();
        }

        public UA(UB Bx)
        {
            InitializeComponent();
            B = Bx;
        }

        public UB B;

        private void UABut_Click(object sender, EventArgs e)
        {
            this.Controls.Clear();
            this.Controls.Add(B);
        }
    }

//UB UserContol
  public partial class UB : UserControl
    {
        public UB()
        {
            InitializeComponent();
        }

        public UB(UA Ax)
        {
            A = Ax;
            InitializeComponent();
        }

        public UA A;

        private void UBBut_Click(object sender, EventArgs e)
        {
            this.Controls.Clear();
            this.Controls.Add(A);
        }
    }
ASKER CERTIFIED SOLUTION
BurnieP

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 10 Answers and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 10 Answers and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros