2

I am new to C#.Net. I have a form with some panels in it. One of the panels is MainPanel. After start up, the MainPanel is empty. Based on user selection, I want to load some controls in it. Something similar to CardLayout in Java! Each panel have a lot of Controls and I don't want to add them programatically. In fact the question is "Is there a way to have some panels designed in designer and show/hide them based on user selection, all in one form?"

enter image description here

Thank you.

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
  • Is this Windows Forms, WPF, Silverlight, ASP.Net? – gregsdennis Jun 14 '12 at 18:43
  • 1
    He tagged Winforms, so most likely that. – krillgar Jun 14 '12 at 18:45
  • I have added a screen capture that may help me to express what I mean. – Ali Behzadian Nejad Jun 14 '12 at 18:47
  • So that top section with the yellow squares, the column below the picture of the guy, and the main section? Yes, they can all be made as UserControls. Make them in their own classes, and then you can add them from the Toolbox in Designer Mode and place them how you want on the form. One other way to make life a little easier would be to put Panels (or SplitContainers) in your form, so that you can layout the size of each Panel. Then add the UserControls to the Panel.Controls property, or drop them in with the Designer. – krillgar Jun 14 '12 at 18:50
  • The white space in bottom-left is MainPanel. I want to change it based on user selection in top-bar buttons with other panels designed before and are ready to use. – Ali Behzadian Nejad Jun 14 '12 at 18:58
  • Then yes, use my Answer. Right click on your project and Add New Item. Select a User Control. Create the new file, and design it how you want. I'll edit my answer to provide you a clearer understanding of what you'll need to do. – krillgar Jun 14 '12 at 19:02

3 Answers3

3

Yes, create new objects called UserControls in their own classes. You can add and remove them programmatically, but create them in the designer.

To keep from getting flicker when changing the controls, do something like the following:

Control ctlOld = frmMain.Controls[0]; // this will allow you to remove whatever control is in there, allowing you to keep your code generic.
ctlNextControl ctl = new ctlNextControl(); // this is the control you've created in another class
frmMain.Controls.Add(ctlNextControl);
frmMain.Controls.Remove(ctlOld);

Create your User Controls, and name them whatever you wish, I'll make some names up now for examples:

ctlGear
ctlMap
ctlGraph
ctlCar
ctlPerson

Add those 5 files as UserControls to your project. Design them however you want.

Create an enum for the different buttons for ease of use:

public enum ControlType {
    Gear,
    Map,
    Graph,
    Car,
    Person
}

Once you have them created, in the button click events for each of those buttons, add a call to this new method:

private void SwitchControls(ControlType pType) {
    // Keep a reference to whichever control is currently in MainPanel.
    Control ctlOld = MainPanel.Controls[0];
    // Create a new Control object
    Control ctlNew = null;
    // Make a switch statement to find the correct type of Control to create.
    switch (pType) {
        case (ControlType.Gear):
           ctlNew = new ctlGear();
           break;
        case (ControlType.Map):
           ctlNew = new ctlMap();
           break;
        case (ControlType.Graph):
           ctlNew = new ctlGraph();
           break;
        case (ControlType.Car):
            ctlNew = new ctlCar();
            break;
        case (ControlType.Person):
            ctlNew = new ctlPerson();
            break;
        // don't worry about a default, unless you have one you would want to be the default.
    }

    // Don't try to add a null Control.
    if (ctlNew == null) return();

    MainPanel.Controls.Add(ctlNew);

    MainPanel.Controls.Remove(ctlOld);
}

Then in your button click events, you could have something like this:

private void btnGear.Click(object sender, EventArgs e) {
    SwitchControls(ControlType.Gear);
}

The same thing would be in the other click events, just change out the ControlType in the parameters.

krillgar
  • 12,596
  • 6
  • 50
  • 86
2

Just set the Visible property on the other panels. However, LarsTech is right that it might be best to swap in user controls to the main panel as you need them

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
1

Try creating these other "panels" as User Controls. You can design them just like you would forms.

Alternatively, you can use this Hans Passant answer where he uses a TabControl but then hides the tabs during runtime: Creating Wizards for Windows Forms in C#

Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225