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);
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Unlimited question asking, solutions, articles and more.
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(); } }
Unlimited question asking, solutions, articles and more.
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.
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