Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#
Tip/Trick

How to Create a Simple Expandable / Collapsible Panel

Rate me:
Please Sign up or sign in to vote.
4.69/5 (17 votes)
1 Nov 2014CPOL1 min read 86.8K   6.5K   25   7
How to create a Simple Expandable / Collapsible Menu Panel for Windows Forms in C#.NET

Introduction

I Googled a lot for C# code to create an expandable menu or panel for my application. I got it but I have to write a large amount of code for that and it was time consuming. Why should we waste our valuable time when there is a simple and easy way to create the same??? I am here to explain my way of creating an Expandable/Collapsible Menu. See the following figure:

Image 1

Using the Code

Follow these steps:

  1. Create a Windows Form application (e.g.: frmMain)
  2. Add a Panel control and dock it anywhere you want (e.g.: pnlMenu)
  3. Add a Panel inside the main panel (e.g.: pnlMenu -> pnlMenuGroup1)
  4. Add a Button or any other control like label/picturebox to the inside panel and dock it to Top - It is the Menu Head. (e.g.: btnMenuGroup1 - Button Height = 25)
  5. Add Button controls as submenus to the inside panel and dock it to Top. Now it will look like a menu group. (e.g.: btnAbout - Button Height = 25)
  6. Create more "menu groups" like you have created now. (Follow steps 1 to 5.)
  7. Add Image (Up/Down) to the Button which is going to be your "menu head".

*** Add Picturebox controls and load images and adjust Transparency to give your window more visual styles.

C#
//
// frmMain

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExpandablePanelSample
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Width = Screen.PrimaryScreen.WorkingArea.Width;
            this.Height = Screen.PrimaryScreen.WorkingArea.Height;
            Left = Top = 0;

            pnlMenuGroup1.Height = 25;
            pnlMenuGroup2.Height = 25;
            pnlMenuGroup3.Height = 25;

            btnMenuGroup1.Image = Properties.Resources.down;
            btnMenuGroup2.Image = Properties.Resources.down;
            btnMenuGroup3.Image = Properties.Resources.down;
        }

        private void btnMenuGroup1_Click(object sender, EventArgs e)
        {
            if (pnlMenuGroup1.Height == 25)
            {
                pnlMenuGroup1.Height = (25 *4) + 2;
                btnMenuGroup1.Image = Properties.Resources.up;
            }
            else
            {
                pnlMenuGroup1.Height = 25;
                btnMenuGroup1.Image = Properties.Resources.down;
            }
        }

        private void btnMenuGroup2_Click(object sender, EventArgs e)
        {
            if (pnlMenuGroup2.Height == 25)
            {
                pnlMenuGroup2.Height = (25 * 4) + 2;
                btnMenuGroup2.Image = Properties.Resources.up;
            }
            else
            {
                pnlMenuGroup2.Height = 25;
                btnMenuGroup2.Image = Properties.Resources.down;
            }
        }

        private void btnMenuGroup3_Click(object sender, EventArgs e)
        {
            if (pnlMenuGroup3.Height == 25)
            {
                pnlMenuGroup3.Height = (25 * 4) + 2;
                btnMenuGroup3.Image = Properties.Resources.up;
            }
            else
            {
                pnlMenuGroup3.Height = 25;
                btnMenuGroup3.Image = Properties.Resources.down;
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            frmAbout frmChild = new frmAbout();
            frmChild.MdiParent = this;
            frmChild.Show();
        }
    }
}

//

For details, download the project sample attached.

Enjoy programming!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelance
India India
He is a Commerce Graduate and working as an Accountant. He learned Programming because of his passion and craze. He is a beginner in .NET. His favourite language is C# and favourite database is MySQL.
He loves playing guitar and listening to music.

Comments and Discussions

 
QuestionHow might I add "grandchildren" to this collapsible panel? Pin
1JDB123-Nov-21 5:17
1JDB123-Nov-21 5:17 
QuestionVery helpful Pin
Ramon Ritter7-Oct-17 23:46
professionalRamon Ritter7-Oct-17 23:46 
QuestionCode Run with Error Pin
Manoj Kumar1-Apr-15 0:23
Manoj Kumar1-Apr-15 0:23 
GeneralMy vote of 4 Pin
srilekhamenon12-Nov-14 0:38
professionalsrilekhamenon12-Nov-14 0:38 
GeneralRe: My vote of 4 Pin
Noble KC12-Nov-14 18:05
professionalNoble KC12-Nov-14 18:05 
QuestionMy vote of 4 Pin
W. Kleinschmit3-Nov-14 3:15
W. Kleinschmit3-Nov-14 3:15 
AnswerRe: My vote of 4 Pin
Noble KC3-Nov-14 20:06
professionalNoble KC3-Nov-14 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.