c# listbox使用


1.listbox在顯示時很重要,尤其涉及到日志輸出,串口輸出,讀取數據庫的內容,結合定時器的使用,就更加好了。

2.所以寫了一小例子,僅供參考,本篇也參考了別人寫的代碼,http://www.cnblogs.com/xielong/p/6744805.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace listbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string peopleText = textBox1.Text.Trim().ToString();
//獲取listbox1的對象
ListBox list1 = this.listBox1;
//判斷人員是否已經添加過
if (!list1.Items.Contains(peopleText))
{
//list1.Items.Add(peopleText);
list1.Items.Insert(0, peopleText);
}
else
{
MessageBox.Show("該人員已經添加過,無法重復添加!");
}
}

private void button2_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("未選中采購人員,無法轉移銷售部門!");
}
}

private void button3_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("未選中銷售人員,無法轉移到采購部門!");
}
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM