C# Winfom 中ListBox的簡(jiǎn)單用法詳解
1、如何添加listBox的值
this.listBox1.Items.Add("張曉東");
2、如何判斷l(xiāng)istBox集合是否添加過
//檢查添加值是否添加過
if(this.listBox1.items.Contains("張曉東")){
MessageBox.show("集合成員已添加過!");
}
else{
//執(zhí)行添加集合成員
}
3、如何獲取listBox選中的值
//判斷所有選中項(xiàng)集合大于0
if(this.listBox1.SelectedItems.Count > 0){
//獲取選中的值
this.listBox1.SelectedItem.ToString();
}
else{
MessageBox.Show("未選中l(wèi)istbox集合的值");
}
4、如何移除listBox中存在的值
//移除listBox集合的項(xiàng)
this.listBox1.Items.Remove("張曉東");
5、綜合使用例子
簡(jiǎn)單實(shí)現(xiàn)人員從部門1轉(zhuǎn)移到部門2或部門2轉(zhuǎn)移到部門1
1)界面設(shè)計(jì)

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的對(duì)象
ListBox list1 = this.listBox1;
//判斷人員是否已經(jīng)添加過
if (!list1.Items.Contains(peopleText))
{
list1.Items.Add(peopleText);
}
else {
MessageBox.Show("該人員已經(jīng)添加過,無法重復(fù)添加!");
}
}
/// <summary>
/// 將采購人員轉(zhuǎn)移到銷售部門
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRightMove_Click(object sender, EventArgs e)
{
//獲取listbox1的所有選中的項(xiàng)
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("該人員已經(jīng)轉(zhuǎn)移過,無法重復(fù)轉(zhuǎn)移!");
}
}
else {
MessageBox.Show("未選中采購人員,無法轉(zhuǎn)移銷售部門!");
}
}
/// <summary>
/// 將銷售人員轉(zhuǎn)移到采購部門
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLeftMove_Click(object sender, EventArgs e)
{
//獲取listbox2的所有選中的項(xiàng)
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("該人員已經(jīng)轉(zhuǎn)移過,無法重復(fù)轉(zhuǎn)移!");
}
}
else
{
MessageBox.Show("未選中銷售人員,無法轉(zhuǎn)移到采購部門!");
}
}
}
}
3)界面演示
3.1)添加人員到部門1演示效果

3.2)部門1轉(zhuǎn)移到部門2演示效果

3.3)部門2轉(zhuǎn)移到部門1演示效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#中CheckedListBox控件的用法實(shí)例
- C# ListBox中的Item拖拽代碼分享
- C#使用checkedListBox1控件鏈接數(shù)據(jù)庫的方法示例
- C# CheckedListBox控件的用法總結(jié)
- C#實(shí)現(xiàn)讓ListBox適應(yīng)最大Item寬度的方法
- C#讀取文本文件到listbox組件的方法
- C#保存listbox中數(shù)據(jù)到文本文件的方法
- C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)
- C#入門教程之ListBox控件使用方法
- c#使用listbox的詳細(xì)方法和常見問題解決
相關(guān)文章
C#?CefSharp?根據(jù)輸入日期段自動(dòng)選擇日期的操作代碼
這篇文章主要介紹了C#?CefSharp?根據(jù)輸入日期段自動(dòng)選擇日期的操作代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-01-01
C#的FileSystemWatcher用法實(shí)例詳解
這篇文章主要介紹了C#的FileSystemWatcher用法,以實(shí)例形似詳細(xì)分析了FileSystemWatcher控件主要功能,并總結(jié)了FileSystemWatcher控件使用的技巧,需要的朋友可以參考下2014-11-11
C#實(shí)現(xiàn)添加多行文本水印到Word文檔
一般情況下,在Word中添加文字水印僅支持添加一個(gè)文本字樣的水印,由于對(duì)不同文檔的設(shè)計(jì)要求,需要在Word文檔中添加平鋪水印效果。本文將介紹如何來實(shí)現(xiàn)該水印效果的方法,感興趣的可以了解一下2022-07-07
C#?WinForm?RichTextBox文本動(dòng)態(tài)滾動(dòng)顯示文本方式
這篇文章主要介紹了C#?WinForm?RichTextBox文本動(dòng)態(tài)滾動(dòng)顯示文本方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
C#使用泛型實(shí)現(xiàn)刪除數(shù)組中重復(fù)元素
這篇文章主要為大家詳細(xì)介紹了C#如何使用泛型實(shí)現(xiàn)刪除數(shù)組中重復(fù)元素,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02

