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);
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
For Now, this is what I`m doing. What`s you opinion? Is there a better / faster way to do this?
Open in new window