C#
--
Questions
--
Followers
Top Experts
Loading UserControls in Panel in C#
I'm new to C# and VS.NET and was having an issue I was hoping someone could help me with.  I have a main form that has some standard information I would always like to display on my application (like a header for a webpage but in this case an application).  I then have a panel on my main form called targetPanel which I was wanting to load UserControls I created into.  Basically the application will load one UserControl on startup and then when clicking a button (located inside that user control) it should be able to load another user control inside this targetPanel. Below is some code I was using to try and do this.
public void ShowControl(UserControl controlName){
targetPanel.Controls.Clear (); // Remove any current controls in the panel 
targetPanel.Controls.Add(c ontrolName ); // Put the special control in the host panel 
controlName.Dock = DockStyle.Fill; // Make the control fill the host panel
}
The problem of course is that every userControl is its own class in C# and already has inheritance with the UserControl class built in with .NET. So how do I have one user control communicate with the MainForm so that it can alter the panel I have on that form called targetPanel so it can load another UserControl based on what was selected?
I would greatly appreciate some assistance as this has been bugging me. Thank you.
public void ShowControl(UserControl controlName){
targetPanel.Controls.Clear
targetPanel.Controls.Add(c
controlName.Dock = DockStyle.Fill; // Make the control fill the host panel
}
The problem of course is that every userControl is its own class in C# and already has inheritance with the UserControl class built in with .NET. So how do I have one user control communicate with the MainForm so that it can alter the panel I have on that form called targetPanel so it can load another UserControl based on what was selected?
I would greatly appreciate some assistance as this has been bugging me. Thank you.
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
ASKER CERTIFIED SOLUTION
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
C#
--
Questions
--
Followers
Top Experts
C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).