首页 > Windows开发 > 详细

C#winform中ListView及ContextMenuStrip的使用

时间:2014-11-06 21:45:55      阅读:514      评论:0      收藏:0      [点我收藏+]

1.新建一个Windows窗体应用程序

bubuko.com,布布扣

 2.添加文件夹及图片资源

文件夹图片bubuko.com,布布扣(folder.png),文件图片bubuko.com,布布扣(file.png)

bubuko.com,布布扣

 3.在设计模式下添加控件

bubuko.com,布布扣

即:1个ListView(lvwData)、2个ImageList(imgLarge,imgSmall),1个ContextMenuStrip(cmsStyle),如下所示:

bubuko.com,布布扣

4.设置各控件详细属性

2个ImageList(imgLarge,imgSmall)添加Images、设置ImageSize(imgLarge--32*32, imgSmall--16*16),如下:

bubuko.com,布布扣

bubuko.com,布布扣  bubuko.com,布布扣

ContextMenuStrip(cmsStyle)添加Items,如下:

bubuko.com,布布扣

ListView(lvwData)要添加ItemsColumns,绑定ContextMenuStrip(cmsStyle)、LargeImageListSmallImageList

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣 

bubuko.com,布布扣

界面设计完成后,锁定所有控件,这是个好习惯~~

bubuko.com,布布扣

 5.为ContextMenuStrip(cmsStyle)的每个子项添加事件处理逻辑

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace Controls
 11 {
 12     public partial class MainForm : Form
 13     {
 14         /// <summary>
 15         /// 默认构造函数
 16         /// </summary>
 17         public MainForm()
 18         {
 19             InitializeComponent();
 20 
 21             this.大图标ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
 22             this.详细信息ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
 23             this.平铺ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
 24             this.小图标ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
 25             this.列表ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click);
 26         }
 27 
 28         #region 鼠标右键勾选事件处理
 29         /// <summary>
 30         /// 设定右键菜单勾选项,设置ListView列表显示样式
 31         /// </summary>
 32         /// <param name="sender"></param>
 33         /// <param name="e"></param>
 34         private void ToolStripMenuItem_Click(object sender, EventArgs e)
 35         {
 36             ToolStripMenuItem tsMenumItem = sender as ToolStripMenuItem;
 37             if (tsMenumItem.Checked)
 38             {
 39                 return;//已经选中则返回
 40             }
 41             else
 42             {
 43                 //清除勾选的右键菜单项
 44                 ClearCheckState(cmsStyle);
 45                 //勾选鼠标选中的右键菜单项
 46                 tsMenumItem.Checked = true;
 47             }
 48             //设置ListView列表显示样式
 49             SetDisplayStyle(tsMenumItem.Text);
 50         }
 51 
 52         /// <summary>
 53         /// 清除勾选的右键菜单项
 54         /// </summary>
 55         /// <param name="cms">右键菜单</param>
 56         private void ClearCheckState(ContextMenuStrip cms)
 57         {
 58             ToolStripMenuItem tsMenumItemTemp;
 59             for (int i = 0; i < cms.Items.Count; i++)
 60             {
 61                 if (!(cms.Items[i] is ToolStripMenuItem))
 62                 {
 63                     continue;
 64                 }
 65                 tsMenumItemTemp = cms.Items[i] as ToolStripMenuItem;
 66                 if (tsMenumItemTemp.Checked)
 67                 {
 68                     tsMenumItemTemp.Checked = false;
 69                 }
 70             }
 71         }
 72 
 73         /// <summary>
 74         /// 设置ListView列表显示样式
 75         /// </summary>
 76         /// <param name="name"></param>
 77         private void SetDisplayStyle(string name)
 78         {
 79             foreach (ListViewItem lvi in listData.Items)
 80             {
 81                 if (name == "详细信息")
 82                 {
 83                     lvi.ImageIndex = 1;
 84                 }
 85                 else
 86                 {
 87                     lvi.ImageIndex = 0;
 88                 }
 89             }
 90             switch (name)
 91             {
 92                 case "大图标":
 93                     listData.View = View.LargeIcon;
 94                     break;
 95                 case "详细信息":
 96                     listData.View = View.Details;
 97                     break;
 98                 case "小图标":
 99                     listData.View = View.SmallIcon;
100                     break;
101                 case "列表":
102                     listData.View = View.List;
103                     break;
104                 case "平铺":
105                     listData.View = View.Tile;
106                     break;
107             }
108         }
109         #endregion
110     }
111 }

6.运行效果如下所示:

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

C#winform中ListView及ContextMenuStrip的使用

原文:http://www.cnblogs.com/makesense/p/4079835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!