0%

C# ListBox左移、右移、上移、下移

实现ListBox中数据移动的方法,网上查到的,留存备用

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
 /// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBoxOperate_OnCommand(object sender, EventArgs e)
{
Button btn = sender as Button;

List<string> list = new List<string>();

switch (btn.Tag.ToString())
{
case "ToRight":
if (listBox1.SelectedItems != null)
{
foreach (var item in listBox1.SelectedItems)
{
listBox2.Items.Add(item.ToString());
}
this.listBox2.SelectedIndex = this.listBox2.Items.Count - 1;
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
i--;
}
}
break;
case "AllToRight":
if (this.listBox1.Items.Count > 0)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
list.Add(listBox1.Items[i].ToString());
}
for (int j = 0; j < list.Count; j++)
{
this.listBox2.Items.Add(list[j]);
}
this.listBox1.Items.Clear();
break;
}
else
{
break;
}
case "ToLeft":

if (listBox2.SelectedItems != null)
{
foreach (var item in listBox2.SelectedItems)
{
listBox1.Items.Add(item.ToString());
}
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
for (int i = 0; i < listBox2.SelectedIndices.Count; i++)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
i--;
}
}
break;
case "AllToLeft":
if (this.listBox2.Items.Count > 0)
{

for (int i = 0; i < listBox2.Items.Count; i++)
{
list.Add(listBox2.Items[i].ToString());
}
for (int j = 0; j < list.Count; j++)
{
this.listBox1.Items.Add(list[j]);
}
this.listBox2.Items.Clear();
break;
}
else
{
break;
}
case "ToUp":
// 上移
if (this.listBox2.SelectedIndices.Count > 0 &&
this.listBox2.SelectedIndices[0] > 0)
{
int[] newIndices =
this.listBox2.SelectedIndices.Cast<int>()
.Select(index => index - 1).ToArray();

this.listBox2.SelectedItems.Clear();

for (int i = 0; i < newIndices.Length; i++)
{
object obj = this.listBox2.Items[newIndices[i]];
this.listBox2.Items[newIndices[i]] = this.listBox2.Items[newIndices[i] + 1];
this.listBox2.Items[newIndices[i] + 1] = obj;
this.listBox2.SelectedItems.Add(this.listBox2.Items[newIndices[i]]);
}
}

break;
case "ToDown":


// 下移
if (this.listBox2.SelectedIndices.Count > 0 &&
this.listBox2.SelectedIndices[this.listBox2.SelectedIndices.Count - 1] <
this.listBox2.Items.Count - 1)
{
int[] newIndices =
this.listBox2.SelectedIndices.Cast<int>()
.Select(index => index + 1).ToArray();

this.listBox2.SelectedItems.Clear();

for (int i = newIndices.Length; i > 0; i--)
{
object obj = this.listBox2.Items[newIndices[i - 1]];
this.listBox2.Items[newIndices[i - 1]] = this.listBox2.Items[newIndices[i-1]-1];
this.listBox2.Items[newIndices[i-1]-1] = obj;
this.listBox2.SelectedItems.Add(this.listBox2.Items[newIndices[i-1]]);

}
}
break;
}
}