Link to home
Start Free TrialLog in
Avatar of San24
San24

asked on

User Control Constructors.

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
Avatar of BurnieP
BurnieP
Flag of Canada 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
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
SOLUTION
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
SOLUTION
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
SOLUTION
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 San24
San24

ASKER

I`m just trying to switch between user controls [back and forth] and then pass parameters between them. I have an approach similar to both of what you suggested. I was just wondering if I could pass the User Control itself in the constructor, so that I can initialize and do some calculations before the parameters are used.

For Now, this is what I`m doing. What`s you opinion? Is there a better / faster way to do this?





//Main Form
   public Form1()
        {
            InitializeComponent();
        }

        private void But_Click(object sender, EventArgs e)
        {
            AB ABobj = new AB();
            ABobj.A.Bx = ABobj.B;
            ABobj.B.Ax = ABobj.A;

            panel1.Controls.Add(ABobj.A);
            panel1.Controls.Add(ABobj.B);

            ABobj.A.Show();
            ABobj.B.Hide();
        }

    }

    public class AB
    {
        public UA A = new UA();
        public UB B = new UB();
    }

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

        public UB Bx;

        private void UABut_Click(object sender, EventArgs e)
        {
            Hide();
            Bx.Show();
        }
    }

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

        public UA Ax;

        private void UBBut_Click(object sender, EventArgs e)
        {
            Hide();
            Ax.Show();
        }
    }

Open in new window

SOLUTION
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
SOLUTION
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 San24

ASKER

Andy - Yes it works. I need to have a seprate class. I can`t have it on the main Form. This is just not merely to organize the code. I have different objects/user controls to load - for simplicity think about it as AB, CD.., XY...depending on what the user chooses. Once that is done, the objects within have to have a switch functionality.

Idle Mind - I agree with the Chicken and Egg comparison, the more I look into it.

What would be the most simplest way?

When I use the word better - I mean using proper programming practices, Unfortunately I`m the only one whose working on this project with no one to give me feed back or help. So, sometimes I tend to question the approach I take.
SOLUTION
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
SOLUTION
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
SOLUTION
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