listbox demo
功能
添加、删除、修改选中的项、上移、下移、清空、保存列表、加载列表、判断内容是否重复、查找、模糊查找、取消选择、上一条、下一条、第一条、最后一条
下载地址:https://download.csdn.net/download/u012663700/11994849
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace ListBoxDemo 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 SetupButtonEnabled(); 22 } 23 24 //设置按钮的可用和不可用 25 private void SetupButtonEnabled() 26 { 27 string s = textBox1.Text.Trim(); 28 int i = listBox1.SelectedIndex; 29 int count = listBox1.Items.Count; 30 31 //添加 32 btnAdd.Enabled = !listBox1.Items.Contains(s) && s.Length > 0 && !s.Contains(","); 33 34 //删除 35 btnDel.Enabled = i != -1; 36 37 //重命名 38 btnRename.Enabled = i != -1 && listBox1.Items[i].ToString() != s; 39 40 //上移 41 btnMoveUp.Enabled = i - 1 >= 0; 42 43 //下移 44 btnMoveDown.Enabled = i + 1 < listBox1.Items.Count && i != -1; 45 46 //第一条 47 btnFirst.Enabled = count > 0 && i != 0; 48 49 //最后一条 50 btnLast.Enabled = i != count - 1; 51 52 //上一条 53 btnPre.Enabled = count > 0 && i != 0 && i != -1; 54 55 //下一条 56 btnNext.Enabled = i != count - 1 && i != -1; 57 58 //搜索 59 btnFind.Enabled = textBox1.Text.Trim().Length > 0; 60 61 //模糊搜索 62 btnFind1.Enabled = btnFind.Enabled; 63 64 //取消选择 65 btn取消选择.Enabled = i != -1; 66 } 67 68 private void 添加文本_Click(object sender, EventArgs e) 69 { 70 string s = textBox1.Text.Trim(); 71 if (s == "") 72 { 73 MessageBox.Show("内容不能为空或空格", "操作取消", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 74 } 75 else 76 { 77 /* 78 * 不添加列表中已经存在的内容, 79 */ 80 listBox1.Items.Add(s); 81 } 82 textBox1.Text = ""; 83 textBox1.Focus(); 84 } 85 86 private void 修改选中的项_Click(object sender, EventArgs e) 87 { 88 /* 89 * 可以通过这样直接修改指定的项的值 listBox1.Items[i] = "指定值"; 90 * 91 */ 92 string s = textBox1.Text; 93 int i = listBox1.SelectedIndex; 94 if (i != -1) 95 { 96 if (listBox1.Items[i].ToString() != s) 97 { 98 listBox1.Items[i] = s; 99 100 s = listBox1.Items[i].ToString(); 101 } 102 else 103 { 104 MessageBox.Show("名字相同", "不用重新命名", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 105 } 106 } 107 } 108 109 private void 删除选中的项_Click(object sender, EventArgs e) 110 { 111 /* 112 * 这里的操作是 一次只删除一项 113 */ 114 int i = listBox1.SelectedIndex; 115 if (i != -1) 116 { 117 DialogResult d = MessageBox.Show("是否确定删除该分组?", "删除操作", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 118 if (d == DialogResult.Yes) 119 { 120 listBox1.Items.RemoveAt(i); 121 textBox1.Text = ""; 122 123 if (listBox1.Items.Count > 0 ) 124 { 125 if (i == listBox1.Items.Count) //这一句是避免抛出异常 126 { 127 i = 0; 128 } 129 listBox1.SelectedIndex = i; 130 listBox1.Focus(); 131 } 132 } 133 } 134 } 135 136 public static void Swap(ref string a, ref string b) 137 { 138 string c = a; 139 a = b; 140 b = c; 141 } 142 143 /* 144 * 上移 和下移 实际上是交换2个数值 145 * 上移就是[当前选中]的项和 [上一个项] 对换下值 146 * 下移就是[当前选中]的项和 [下一个项] 对换下值 147 * 148 */ 149 150 private void btnMoveUp_Click(object sender, EventArgs e) 151 { 152 //[上移] 153 string s = textBox1.Text; 154 int i = listBox1.SelectedIndex; 155 if (i != -1) 156 { 157 if (i - 1 >= 0) 158 { 159 string a = listBox1.Items[i].ToString(); 160 string b = listBox1.Items[i - 1].ToString(); 161 Swap(ref a, ref b); 162 listBox1.Items[i] = a; 163 listBox1.Items[i - 1] = b; 164 listBox1.SelectedIndex = i - 1; 165 } 166 } 167 } 168 169 private void btnMoveDown_Click(object sender, EventArgs e) 170 { 171 //[下移] 172 string s = textBox1.Text; 173 int i = listBox1.SelectedIndex; 174 if (i != -1) 175 { 176 if (i + 1 < listBox1.Items.Count) 177 { 178 string a = listBox1.Items[i].ToString(); 179 string b = listBox1.Items[i + 1].ToString(); 180 Swap(ref a, ref b); 181 listBox1.Items[i] = a; 182 listBox1.Items[i + 1] = b; 183 listBox1.SelectedIndex = i + 1; 184 } 185 } 186 } 187 188 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 189 { 190 object obj = listBox1.SelectedItem; 191 string s = obj != null ? obj.ToString() : ""; //listbox如果没有选中则返回null 192 textBox1.Text = s; 193 SetupButtonEnabled(); 194 } 195 196 private void textBox1_TextChanged(object sender, EventArgs e) 197 { 198 //当文本框中的内容和选中的内容一样则不用重命名 199 SetupButtonEnabled(); 200 } 201 202 203 //用来存放listBox的内容 204 string myStr = ""; 205 private void btnSave_Click(object sender, EventArgs e) 206 { 207 /* 208 * 将listBox的值转换成这种格式 aa,bb,cc,ddd,ee,ff 然后写入文件 209 * ,为分割符,为了避免把逗号号进来, SetupButtonEnabled中设置了如果内容包含,则不能添加 210 */ 211 string s = ""; 212 StringBuilder sb = new StringBuilder(); 213 int length = listBox1.Items.Count; 214 for (int i = 0; i < length; i++) 215 { 216 s = listBox1.Items[i].ToString(); 217 sb.Append(s + ","); 218 } 219 s = sb.ToString(); 220 if (s.EndsWith(",")) 221 s = s.Substring(0, s.Length - 1); 222 myStr = s; 223 MessageBox.Show(s); 224 } 225 226 private void btnRead_Click(object sender, EventArgs e) 227 { 228 /* 229 * 读取 230 * 将这样的 aa,bb,cc,ddd,ee,ff 字符串 转换成字符串数组然后 添加到listBox中 231 */ 232 string[] arr = myStr.Split(new char[] { ',' }); 233 listBox1.Items.Clear(); 234 listBox1.Items.AddRange(arr); 235 } 236 237 private void listBox1_MouseDown(object sender, MouseEventArgs e) 238 { 239 //双击删除选中的项 240 int i = listBox1.SelectedIndex; 241 if (i != -1) 242 { 243 if (e.Clicks == 2) 244 { 245 listBox1.Items.RemoveAt(i); 246 } 247 } 248 } 249 250 private void 清空_Click(object sender, EventArgs e) 251 { //清空 252 listBox1.Items.Clear(); 253 } 254 255 private void 第一条_Click(object sender, EventArgs e) 256 { 257 if (listBox1.Items.Count > 0) 258 { 259 listBox1.SelectedIndex = 0; 260 label1.Text = listBox1.SelectedItem.ToString(); 261 } 262 263 SetupButtonEnabled(); 264 } 265 266 private void 上一条_Click(object sender, EventArgs e) 267 { 268 if (listBox1.SelectedIndex - 1 >= 0) 269 { 270 int i = listBox1.SelectedIndex; 271 i--; 272 listBox1.SelectedIndex = i; 273 274 label1.Text = listBox1.SelectedItem.ToString(); 275 } 276 SetupButtonEnabled(); 277 } 278 279 private void 下一条_Click(object sender, EventArgs e) 280 { 281 if (listBox1.SelectedIndex + 1 <= listBox1.Items.Count) 282 { 283 int i = listBox1.SelectedIndex; 284 i++; 285 listBox1.SelectedIndex = i; 286 287 label1.Text = listBox1.SelectedItem.ToString(); 288 } 289 SetupButtonEnabled(); 290 } 291 292 private void 最后一条_Click(object sender, EventArgs e) 293 { 294 if ( listBox1.Items.Count >0) 295 { 296 listBox1.SelectedIndex = listBox1.Items.Count - 1; 297 label1.Text = listBox1.SelectedItem.ToString(); 298 } 299 SetupButtonEnabled(); 300 } 301 302 private void 查找内容_Click(object sender, EventArgs e) 303 { 304 /*精确搜索*/ 305 string s = textBox1.Text; 306 int i = listBox1.Items.IndexOf(s); // int i = listBox1.FindStringExact(s); 307 if (i != -1) 308 { 309 listBox1.SelectedIndex = i; 310 } 311 else 312 { 313 MessageBox.Show("列表中没有此项"); 314 } 315 } 316 317 private void 模糊查询_Click(object sender, EventArgs e) 318 { 319 //模糊查询 只要项目包含关键字就行 320 string s = textBox1.Text; 321 bool b = false; 322 for (int i = 0; i < listBox1.Items.Count; i++) 323 { 324 b = false; 325 string x = listBox1.Items[i].ToString(); 326 if (x.Contains(s)) 327 { 328 listBox1.SelectedIndex = i; 329 b = true; 330 break; 331 } 332 } 333 if (b == false) 334 { 335 MessageBox.Show("列表中没有此项"); 336 } 337 } 338 339 private void button1_Click(object sender, EventArgs e) 340 { 341 listBox1.SelectedIndex = -1; 342 } 343 } 344 }