日期:2014-05-17  浏览次数:21066 次

怎么在ListView里添加不同的右键菜单
添加单个的菜单我知道怎么添加,但是针对ListView不同的组添加不同的多个右键菜单就难到了。首先说明一下是动态添加,不是拖入控件的方式
       ContextMenuStrip cmsA = new ContextMenuStrip();
       cmsA.ImageList = this.toolIcon;
       /* 添加A菜单*/
       this.ContextMenuStrip = cmsA;
       cmsA.Opening += new CancelEventHandler(cmsA_Opening);

       ContextMenuStrip cmsB = new ContextMenuStrip();
       cmsA.ImageList = this.toolIcon;
       /* 添加B菜单*/
       this.ContextMenuStrip = cmsB;
       cmsB.Opening += new CancelEventHandler(cmsB_Opening);

       ContextMenuStrip cmsC = new ContextMenuStrip();
       cmsC.ImageList = this.toolIcon;
       /* 添加C菜单*/
       this.ContextMenuStrip = cmsC;
       cmsC.Opening += new CancelEventHandler(cmsC_Opening);
不知道是不是这一句有问题?this.ContextMenuStrip =。。。,在ListView中引用的时候是判断是在A组就调用A菜单,依此类推,我应该怎么写?

------解决方案--------------------
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 WinFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.Columns.Add("Item", 500);
            listView1.Items.Add("1");
            listView1.Items.Add("2");
            ContextMenuStrip cms =