Solved

Loading UserControls in Panel in C#

Posted on 2006-11-03
1
4,428 Views
Last Modified: 2012-06-27
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(controlName); // 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.
0
Comment
Question by:nconfer
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 235 total points
ID: 17868956
"UserControls I created"

Create custom events in your UserControl to communicate information back to the main form.  This is done with Delegates in C#...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 122
coded character set iptc field within a jpeg 1 69
Name space syntax error 12 43
Web Form VB.Net  import CSV 4 26
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now