C# Winfom 中ListBox的简单用法详解
作者:小小邪 时间:2023-03-14 20:38:47
1、如何添加listBox的值
this.listBox1.Items.Add("张晓东");
2、如何判断listBox集合是否添加过
//检查添加值是否添加过
if(this.listBox1.items.Contains("张晓东")){
MessageBox.show("集合成员已添加过!");
}
else{
//执行添加集合成员
}
3、如何获取listBox选中的值
//判断所有选中项集合大于0
if(this.listBox1.SelectedItems.Count > 0){
//获取选中的值
this.listBox1.SelectedItem.ToString();
}
else{
MessageBox.Show("未选中listbox集合的值");
}
4、如何移除listBox中存在的值
//移除listBox集合的项
this.listBox1.Items.Remove("张晓东");
5、综合使用例子
简单实现人员从部门1转移到部门2或部门2转移到部门1
1)界面设计
2)完整源码
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
标签:C#,Winfom,ListBox
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java利用cors实现跨域请求实例
2023-02-24 14:57:35
linux环境下java程序打包成简单的hello world输出jar包示例
2023-11-26 11:11:37
![](https://img.aspxhome.com/file/2023/8/60378_0s.png)
Android模拟实现网易新闻客户端
2022-09-03 18:19:11
![](https://img.aspxhome.com/file/2023/0/137770_0s.jpg)
Java解除文件占用即Dom4j操作后实现xml关流
2022-07-05 19:18:35
Android实现屏幕保持常亮功能
2023-08-09 13:20:15
java如何读取超大文件
2022-10-24 08:20:54
![](https://img.aspxhome.com/file/2023/4/132174_0s.jpg)
Java设计模式之备忘录模式(Memento模式)介绍
2023-08-23 14:26:42
![](https://img.aspxhome.com/file/2023/8/58438_0s.png)
spring boot项目快速构建的全步骤
2023-11-16 21:16:38
![](https://img.aspxhome.com/file/2023/9/110809_0s.png)
一文带你了解C#中抽象方法与虚方法的区别
2023-07-23 00:14:32
详解Java编程中包package的内容与包对象的规范
2022-03-06 17:48:08
Java毕业设计实战之校园一卡通系统的实现
2022-11-26 06:32:56
![](https://img.aspxhome.com/file/2023/1/72521_0s.jpg)
Spring Boot配置Thymeleaf(gradle)的简单使用
2023-04-18 07:41:42
![](https://img.aspxhome.com/file/2023/0/118810_0s.png)
Java多线程Future松获取异步任务结果轻松实现
2022-11-22 15:49:38
![](https://img.aspxhome.com/file/2023/9/89589_0s.jpg)
C语言文件操作之fread函数详解
2023-07-06 18:24:15
![](https://img.aspxhome.com/file/2023/7/103077_0s.jpg)
java并发之原子操作类和非阻塞算法
2022-04-13 04:39:01
直接在线预览Word、Excel、TXT文件之ASP.NET
2021-10-07 15:37:54
一篇文章教你如何用多种迭代写法实现二叉树遍历
2023-12-23 04:03:29
c#中XML解析文件出错解决方法
2022-01-21 00:38:50
Android使用ShareSDK实现应用分享的功能
2023-10-02 06:37:42
![](https://img.aspxhome.com/file/2023/9/137299_0s.gif)
JAVA中SpringBoot启动流程分析
2021-07-03 03:57:05
![](https://img.aspxhome.com/file/2023/2/116112_0s.png)