您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#Winfom中ListBox怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1、如何添加listBox的值
this.listBox1.Items.Add("張曉東");
2、如何判斷l(xiāng)istBox集合是否添加過(guò)
//檢查添加值是否添加過(guò)if(this.listBox1.items.Contains("張曉東")){ MessageBox.show("集合成員已添加過(guò)!"); }else{ //執(zhí)行添加集合成員}
3、如何獲取listBox選中的值
//判斷所有選中項(xiàng)集合大于0if(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
完整源碼
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> /// 添加人員到采購(gòu)部門 /// </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)添加過(guò) if (!list1.Items.Contains(peopleText)) { list1.Items.Add(peopleText); } else { MessageBox.Show("該人員已經(jīng)添加過(guò),無(wú)法重復(fù)添加!"); } } /// <summary> /// 將采購(gòu)人員轉(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)移過(guò),無(wú)法重復(fù)轉(zhuǎn)移!"); } } else { MessageBox.Show("未選中采購(gòu)人員,無(wú)法轉(zhuǎn)移銷售部門!"); } } /// <summary> /// 將銷售人員轉(zhuǎn)移到采購(gòu)部門 /// </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)移過(guò),無(wú)法重復(fù)轉(zhuǎn)移!"); } } else { MessageBox.Show("未選中銷售人員,無(wú)法轉(zhuǎn)移到采購(gòu)部門!"); } } }}
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#Winfom中ListBox怎么用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。