You've probably seen Accordion menus being used on some websites to show and hide content by collapsing panels to reveal content. In this tutorial I'll explain how to create a custom control that inherits from the Panel class to create a simple Accordion menu to contain a group of Buttons.The image on the left shows what the Accordion menu will look like. Notice Sub Menu's have a parent menu.
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.Drawing;
- namespace xUI
- {
- class Menu
- {
- private string _title;
- private List<Control> _controlCollection = new List<Control>();
- private Color _backgroundColor;
- private Color _foreColor;
- private Font _font;
- private ContentAlignment _textAlignment;
- public string Title
- {
- get { return this._title; }
- set { this._title = value; }
- }
- public List<Control> Controls
- {
- get { return this._controlCollection; }
- }
- public Color BackgroundColor
- {
- get { return this._backgroundColor; }
- set { this._backgroundColor = value; }
- }
- public Color ForeColor
- {
- get { return this._foreColor; }
- set { this._foreColor = value; }
- }
- public Font TextFont
- {
- get { return this._font; }
- set { this._font = value; }
- }
- public ContentAlignment TextAlign
- {
- get { return this._textAlignment; }
- set { this._textAlignment = value; }
- }
- }
- }
As you can see in the class, there are a few public properties, which will be used to describe each Menu such as the title and the appearance of the menu. Also notice there is a generic List collection which will hold all of the controls to display for each menu.
With the menu class created, we need to add it to the Accordion class and because the Accordion can have multiple menus, we need a collection to store each menu. Let's have a quick look at an excerpt of the Accordion class supplied in the demo.
- private List<Menu> _menuCollection = new List<Menu>();
- public List<Menu> Menu
- {
- get { return this._menuCollection; }
- }
Notice there is a generic List collection which holds objects of Type Menu and a public property Menu that returns the collection. Now the accordion can hold menus but it can't render them on the form. We need a method that can loop through each menu, create a header control and place any sub controls under the menu. Let's create a new method called Bind in the Accordion class which will bind all the menus and its sub controls to a hosted control. The hosted control in this case is a Panel and as the Accordion class inherits from a Panel class, we can add the menus to its controls using the Controls property. The Bind method should loop through all the Menus stored in the List<Menu> collection. For each menu heading, it should add a new Label control. After creating a Label control for the menu heading, a Panel needs to be created to hold the menu's collapsible content. It is this Panel that we will show/hide when the menu is clicked. Finally we need to add a click event to the Label control and the event should check if the collapsible Panel is visible or not. Let's have a look at the Bind method from the sample demo.
- public void Bind()
- {
- int panelHeight = 0;
- this._menuCollection.Reverse();
- foreach (Menu menu in this._menuCollection)
- {
- Label lblTitle = new Label();
- Panel menuPanel = new Panel();
- lblTitle.Text = menu.Title;
- lblTitle.BackColor = menu.BackgroundColor;
- lblTitle.ForeColor = menu.ForeColor;
- lblTitle.Font = menu.TextFont;
- lblTitle.TextAlign = menu.TextAlign;
- lblTitle.Dock = DockStyle.Top;
- lblTitle.Click += new EventHandler(lblTitle_Click);
- menu.Controls.Reverse();
- foreach (Control control in menu.Controls)
- {
- control.Dock = DockStyle.Top;
- menuPanel.Controls.Add(control);
- }
- menuPanel.Dock = DockStyle.Top;
- menuPanel.AutoSize = true;
- menuPanel.Visible = false;
- this.Controls.Add(menuPanel);
- this.Controls.Add(lblTitle);
- panelHeight += lblTitle.Height;
- }
- this.Height = panelHeight;
- }
Notice how the Bind method iterates over the Menu collection. For each menu its created a Label and a Panel Control, which gets added to the hosted control. When the Label control is created a few properties are set mainly for the menu appearance. All the controls are docked at the top of the hosted control. The menu's sub controls are added to the Panel, which in tern is added to the hosted control. Finally there is a click event which will check the state of each menu to determine weather to show/hide its sub controls.
- void lblTitle_Click(object sender, EventArgs e)
- {
- this.AutoSize = true;
- Label menu = (Label)sender;
- int index = this.Controls.GetChildIndex(menu) -1;
- if (this.Controls[index].Visible)
- {
- this.Controls[index].Visible = false;
- }
- else
- {
- this.Controls[index].Visible = true;
- }
- }
Above is the click event handler for the menu. It gets the index position of the menu, which by this point is hosted in the parent Panel. Because the menu collection was reversed when adding them to the hosted Panel, we decrement the index position of the menu, which will give use the location of it's sub control Panel. We can now use this position to access the Panel control for each menu and check the visibility state, turning it on and off as needed. You can download the sample application from the link below.
Download For C#
No comments:
Post a Comment