C#制作折叠式二级菜单
刚刚开始工作,经理给了一个任务,主要是考验自已水平,让用C#实现一个折叠二级菜单,就向QQ或OutLooK那种,经过一天的努力也算是要做好了,现在总结一下用到的原理
原理:利用panel容器控件做为存放的最底层,然后再放上几个按钮做为一级菜单项,再放一个ListView让它根据单击按钮的不同动态的加载二级菜单项,另外还加几个ImageList用来存放二级菜单的图标.
所有的控件都包含有一个Controls属性,它用于包含控件的集合,这就允许编写代码,迭代Contro集合,使用Control的属性和方法处理或查询每个控件.这个Controls属性也是动态的,可以用于在运行期间在窗体或控件上添加或删除控件
利用Controls就可以动态的生成菜单项
控件都有一个Dock属性,此属性决定控件怎么停靠在父控件上,它的值是一个DockStyle枚举值(Bottom,left,right,None,Fill,Top)
控件都有一个BringToFront()方法,作用是让当前控件显示在最前面.
listView1.LargeImageLis:获取或设置当前项显示大图标用到的ImageList集合.
源代码这是网上找的
/*
* Created with SharpDevelop.
* User: Gary Perkin
* Date: 02/03/2005
* Time: 08:31
*
*/
using System;
using System.Windows.Forms;
namespace DefaultNamespace
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button buttonMisc;
private System.Windows.Forms.ListView listView2;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Button buttonCars;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.ImageList imageListOutlook;
private System.Windows.Forms.ImageList imageListZip;
private System.Windows.Forms.ImageList imageListCars;
private System.Windows.Forms.Button buttonZip;
private System.Windows.Forms.Splitter splitter1;
public MainForm()
{
InitializeComponent();
CreateOutlookList();
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.splitter1 = new System.Windows.Forms.Splitter();
this.buttonZip = new System.Windows.Forms.Button();
this.imageListCars = new System.Windows.Forms.ImageList(this.components);
this.imageListZip = new System.Windows.Forms.ImageList(this.components);
this.imageListOutlook = new System.Windows.Forms.ImageList(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.columnHeader1 = new System