Designing Windows.Form with multiple panels -> How to hide one panel (like a PS layer)

13,157

Solution 1

What you describe is a wizard, and you might want to investigate the approach from Eric J.

However, when I have cases where I want to have multiple panels in the same space within my UI and I want to switch between them in the designer, I like to use a TabControl and hide the tabs on the TabControl. This makes the UI easier to manage at design time and the code is pretty simple to switch between the tabs at run time.

I made a custom control that derives from TabControl called HiddenTabsControl that is very simple. The class only overrides the WndProc and lets the TabControl base class handle everything else. All you need to do is:

  • Add a New Item to your project
  • Choose Custom Control,
  • Name it something like HiddenTabsControl.
  • Change the base Class to TabControl, remove the Constructor and the OnPaint override that Visual Studio added.
  • Copy this override for WndProc into the class:

    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode)
        {
            m.Result = (IntPtr)1;
        }
        else
        {
            base.WndProc(ref m);
        }
    }  
    

Now you can change tabs in the designer and design the UI easily and in the code you can handle events to change tabs as needed. Changing the Selected tab is easily done with:

this.hiddenTabsControl.SelectedTab = this.tabPageYouWantVisible;

One side effect of removing the tabs is the space that the tabs occupy when the control is constructed. Removing them will make the space the HiddenTabsControl occupies change by shrinking it. I usually set the Anchor of the HiddenTabsControl to bottom to keep it from shrinking.

Solution 2

I used this Wizard code in a recent project and it worked well.

It provides the basic experience you are after.

Solution 3

Another less elegant, but quick hack, approach is to simply not add the panel to the parent form until runtime. In doing that, the designer has no idea where the panel belongs prior to compilation, and it won't be displayed.

For example, find the block of code where you add controls to the parent form:

                //this->Controls->Add(this->panel_X);
        this->Controls->Add(this->tabControl);
        this->Controls->Add(this->menuStrip_topMenu);

Comment or remove the statement, then find the handle to the event that occurs when the form is loaded:

        this->Load += gcnew System::EventHandler(this, &MainForm::MainForm_Load);

Then in the definition of the event handler, add the control to the form:

System::Void MainForm_Load(System::Object^  sender, System::EventArgs^  e) {
...
...
this->Controls->Add(this->panel_X);
}

I haven't experienced any unwanted side effects by doing this, but if anyone has a good reason to not I'd be interested in hearing it.

Share:
13,157
Valeria
Author by

Valeria

Updated on June 17, 2022

Comments

  • Valeria
    Valeria about 2 years

    How can I hide one panel in Visual Studio 2008 Form Designer like a layer in PS? Otherwise, can someone recommend another better method to design multiple "screens" that must be clicked through by the user?