首页 > 代码库 > C#:列表视图操作类

C#:列表视图操作类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace Common
{
    /// <summary>
    /// 列表视图操作类
    /// </summary>
    public class CtlListViewOperate
    {
        private ListView m_listView = null;
 
        /// <summary>
        /// 关联列表视图控件
        /// </summary>
        public ListView refListViewControl
        {
            set
            {
                m_listView = value;
            }
            get
            {
                return m_listView;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="listView">树视图控件</param>
        public CtlListViewOperate()
        {
        }
 
        //public CtlListViewOperate(ListView listView)
        //{
        //    m_listView = listView;
        //}
 
        private static volatile CtlListViewOperate m_dgvOpera = null;
 
        /// <summary>
        /// 获取列表视图操作类单一实例
        /// </summary>
        /// <returns></returns>
        public static CtlListViewOperate GetInstance()
        {
            if(null == m_dgvOpera)
            {
                m_dgvOpera = new CtlListViewOperate();
            }
            return m_dgvOpera;
        }
 
        /// <summary>
        /// 删除选中的项
        /// </summary>
        public void DeleteItems()
        {
            if (MessageBox.Show("确定要删除选中的项吗?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int num = m_listView.CheckedItems.Count;
                for (int i = num - 1; i >= 0; i--)             //从大到小序列依次删除  否则 删除 报错 因为 视图项是动态的
                {
                    m_listView.Items.Remove(m_listView.CheckedItems[i]);
                }
            }
        }
 
        /// <summary>
        /// 添加新项
        /// </summary>
        /// <param name="name">名</param>
        /// <param name="value">值</param>
        public void AddItem(string name, string value)
        {
            int num = m_listView.Items.Count;
            for (int i = 0; i < num; i++ )
            {
                if ((m_listView.Items[i].Tag).ToString() == value)
                {
                    MessageBox.Show("该项已经存在!");
                    return;
                }
            }
            ListViewItem lvi = new ListViewItem();
            lvi.Text = name;
            lvi.Tag = value;
            m_listView.Items.Add(lvi);
        }
        /// <summary>
        /// 添加新项
        /// </summary>
        /// <param name="lvi">视图子项</param>
        public void AddItem(ListViewItem lvi)
        {
            m_listView.Items.Add(lvi);
        }
 
    }
}