Sign in to follow this  
David Thompson

[.net] C# Loading New Panel Within A Form

Recommended Posts

Here is what I am trying to do. I have a form built with 2 panels, the one on the bottom has a bunch of controls that will not ever change, kind of a persistant HUD. Above that, the rest of the screen is another panel, which will frequently change, and contain all kinds of various windows form components such as buttons, textboxes, and radio buttons. What is displayed in the top will change based on what is done in the bottom form. For example, in the bottom, I have Previous Question, and Next Question buttons (for a puzzle game). I would like to be able to load an entirely new top panel (or just replace the contents of the one I have) everytime I click a button to change questions. Here is the problem. I know how I can dynamically add form controls at runtime. However, I want to be able to change and tweak things for the panel in the design editor (Visual Studio 2005). What I am currently doing is, designing the panels, and then pasting the designer code into a function, such as CreatePanelNumber27(). The problem with that is, if I decide I want to change panel number 27, I have to edit it, and then recopy the designer code to my functions. Is there a way to just save a panel seperately, like you would a form? I'm sure this was not phrased well, and I will edit for clarity upon request. I appreciate the help as always guys. Thank you, David Thompson

Share this post


Link to post
Share on other sites
Its simple:

create a new class derived from panel:

public class myPanel : Panel
{
...
}

Set its default values as you wish (size, color, docking,etc.

Create one of it in the "mother window", lets say:

public class Form1 : Form
{
Panel BottomPanel; //the hud;
myPanel TopPanel; //displaying the actual content;
}

//of course youcan do this using the visual editor.

If you want a new panel, derive it from your myPanel class, and design it as you wish (add controls, functionality, etc.

public class qestion17Panel : myPanel
{
...
}

In the mother window, when you click the button, that loads the next panel, do this:

btnNextQuestion_Click(object sender, EventArgs e)
{
if(TopPanel!=null)
{
TopPanel.Dispose(); //just to save memory and resources
}
TopPanel = new question17Panel(); //i think this way it works, maybe you will have to use the Form.Controls.Add and Remove methods. Never tried this stuff.
}
Another easy way is using a tabpanel

Or if you wish to make it more elegant, you may create an array of these myPanel derived panels, and display one of them according to the current index:

Share this post


Link to post
Share on other sites
I like your suggestion, it seems like it is probably a much more efficient way to do it. I will switch over to that soon I think.

I have found another solution too, although I think it may be fairly memory inefficient. I just design each of my panels in a new windows form (they each ahve their own designer screen). And when I want to load a new panel on my main form, I just create an instance of that form, and copy the panels components to the panel I already have. Here is an example, assume frmPanelNumber23 is a form, containing just the panel designed as I want, to load into the main form.



pnlMain.Controls.Clear();
frmPanelNumber23 temp = new frmPanelNumber23();
foreach (Control c in temp.Controls)
pnlMain.Controls.Add(c);
temp.Dispose();



Thats a nice lazy way to do it I found. I may switch to your suggestion though. Thank you for the reply.

David Thompson

Share this post


Link to post
Share on other sites
This is really what a UserControl is for. You can design them just like you do a form. They'll show up in the toolbox and you can drag-n-drop them into your form. Or you could construct them at runtime if you prefer. It would be a lot less hassle.

The controls on the UserControl will be scoped private by default. So if you want to control them from the MainForm, you can change their "Modifiers" property to public.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this