1、如何添加listbox的值
1
|
this .listbox1.items.add( "张晓东" ); |
2、如何判断listbox集合是否添加过
1
2
3
4
5
6
7
|
//检查添加值是否添加过 if ( this .listbox1.items.contains( "张晓东" )){ messagebox.show( "集合成员已添加过!" ); } else { //执行添加集合成员 } |
3、如何获取listbox选中的值
1
2
3
4
5
6
7
8
|
//判断所有选中项集合大于0 if ( this .listbox1.selecteditems.count > 0){ //获取选中的值 this .listbox1.selecteditem.tostring(); } else { messagebox.show( "未选中listbox集合的值" ); } |
4、如何移除listbox中存在的值
1
2
|
//移除listbox集合的项 this .listbox1.items.remove( "张晓东" ); |
5、综合使用例子
简单实现人员从部门1转移到部门2或部门2转移到部门1
1)界面设计
2)完整源码
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
|
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace windowsforms { public partial class form3 : form { public form3() { initializecomponent(); } /// <summary> /// 添加人员到采购部门 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btninsert_click( object sender, eventargs e) { //获取添加人的值 string peopletext = this .txtpeople.text.trim().tostring(); //获取listbox1的对象 listbox list1 = this .listbox1; //判断人员是否已经添加过 if (!list1.items.contains(peopletext)) { list1.items.add(peopletext); } else { messagebox.show( "该人员已经添加过,无法重复添加!" ); } } /// <summary> /// 将采购人员转移到销售部门 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnrightmove_click( object sender, eventargs e) { //获取listbox1的所有选中的项 if ( this .listbox1.selecteditems.count > 0) { string checkpeople = this .listbox1.selecteditem.tostring(); //判断是否添加到listbox2 if (! this .listbox2.items.contains(checkpeople)) { //添加人员到listbox2中 this .listbox2.items.add(checkpeople); //移除listbox1中 this .listbox1.items.remove(checkpeople); } else { messagebox.show( "该人员已经转移过,无法重复转移!" ); } } else { messagebox.show( "未选中采购人员,无法转移销售部门!" ); } } /// <summary> /// 将销售人员转移到采购部门 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnleftmove_click( object sender, eventargs e) { //获取listbox2的所有选中的项 if ( this .listbox2.selecteditems.count > 0) { string checkpeople = this .listbox2.selecteditem.tostring(); //判断是否添加到listbox1 if (! this .listbox1.items.contains(checkpeople)) { //添加人员到listbox1中 this .listbox1.items.add(checkpeople); //移除listbox1中 this .listbox2.items.remove(checkpeople); } else { messagebox.show( "该人员已经转移过,无法重复转移!" ); } } else { messagebox.show( "未选中销售人员,无法转移到采购部门!" ); } } } } |
3)界面演示
3.1)添加人员到部门1演示效果
3.2)部门1转移到部门2演示效果
3.3)部门2转移到部门1演示效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/xielong/p/6744805.html