自定义控件 动态折叠二级菜单

这篇博客介绍了如何使用C#实现动态折叠二级菜单,类似QQ或Outlook的效果。作者利用Panel容器、按钮和ListView控件,结合ImageList显示图标。通过Controls属性动态生成菜单项,调整Dock属性实现布局,利用BringToFront()方法调整控件层级。博客还提供了源代码,包括动态增加控件和根据按钮点击加载不同二级菜单的方法。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值