Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 388
Nona Urbiz Avatar asked Nov 29 '22 20:11

Nona Urbiz


2 Answers

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.

like image 186
Jeff Avatar answered Dec 01 '22 08:12

Jeff


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

It provides the basic experience you are after.

like image 36
Eric J. Avatar answered Dec 01 '22 10:12

Eric J.