-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathListViewUtil.cs
180 lines (152 loc) · 4.93 KB
/
ListViewUtil.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace DigitalPlatform.Forms
{
public static class ListViewUtil
{
// 查找一个事项
public static ListViewItem FindItem(ListView listview,
string strText,
int nColumn)
{
foreach (ListViewItem item in listview.Items)
{
string strThisText = GetItemText(item, nColumn);
if (strThisText == strText)
return item;
}
return null;
}
public static void BeginSelectItem(Control control, ListViewItem item)
{
control.BeginInvoke(new Action<ListViewItem>(
(o) =>
{
o.Selected = true;
o.EnsureVisible();
}), item);
}
#if NO
public static int GetColumnHeaderHeight(ListView list)
{
API.RECT rc = new API.RECT();
IntPtr hwnd = API.SendMessage(list.Handle, API.LVM_GETHEADER, 0, 0);
if (hwnd == null)
return -1;
if (API.GetWindowRect(new HandleRef(null, hwnd), out rc))
{
return rc.bottom - rc.top;
}
return -1;
}
#endif
// 获得列标题宽度字符串
public static string GetColumnWidthListString(ListView list)
{
string strResult = "";
for (int i = 0; i < list.Columns.Count; i++)
{
ColumnHeader header = list.Columns[i];
if (i != 0)
strResult += ",";
strResult += header.Width.ToString();
}
return strResult;
}
// 设置列标题的宽度
// parameters:
// bExpandColumnCount 是否要扩展列标题到足够数目?
public static void SetColumnHeaderWidth(ListView list,
string strWidthList,
bool bExpandColumnCount)
{
string[] parts = strWidthList.Split(new char[] { ',' });
if (bExpandColumnCount == true)
EnsureColumns(list, parts.Length, 100);
for (int i = 0; i < parts.Length; i++)
{
if (i >= list.Columns.Count)
break;
string strValue = parts[i].Trim();
int nWidth = -1;
try
{
nWidth = Convert.ToInt32(strValue);
}
catch
{
break;
}
if (nWidth != -1)
list.Columns[i].Width = nWidth;
}
}
// 确保列标题数量足够
public static void EnsureColumns(ListView listview,
int nCount,
int nInitialWidth = 200)
{
if (listview.Columns.Count >= nCount)
return;
for (int i = listview.Columns.Count; i < nCount; i++)
{
string strText = "";
// strText = Convert.ToString(i);
ColumnHeader col = new ColumnHeader();
col.Text = strText;
col.Width = nInitialWidth;
listview.Columns.Add(col);
}
}
// 获得一个单元的值
public static string GetItemText(ListViewItem item,
int col)
{
if (col == 0)
return item.Text;
if (col >= item.SubItems.Count)
return "";
return item.SubItems[col].Text;
}
// 修改一个单元的值
public static void ChangeItemText(ListViewItem item,
int col,
string strText)
{
// 确保线程安全
if (item.ListView != null && item.ListView.InvokeRequired)
{
item.ListView.BeginInvoke(new Action<ListViewItem, int, string>(ChangeItemText), item, col, strText);
return;
}
if (col == 0)
{
item.Text = strText;
return;
}
// 保险
while (item.SubItems.Count < col + 1) // 原来为<=, 会造成多加一列的后果 2006/10/9 changed
{
item.SubItems.Add("");
}
item.SubItems[col].Text = strText;
}
public static void DeleteSelectedItems(ListView list)
{
int[] indices = new int[list.SelectedItems.Count];
list.SelectedIndices.CopyTo(indices, 0);
list.BeginUpdate();
for (int i = indices.Length - 1; i >= 0; i--)
{
list.Items.RemoveAt(indices[i]);
}
list.EndUpdate();
}
}
}