C#學習筆記:ListBox控件的用法


樣式如下:

實現的代碼一:

 

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;
using System.Collections;


namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//實現">"功能,將左邊一項移到右邊
private void button1_Click(object sender, EventArgs e)
{
//listBox1.Items.RemoveAt(listBox1.SelectedIndex);
//listBox2.Items.Add(listBox1.SelectedIndex.ToString());
//獲取listbox1的所有選中的項
if (this.listBox1.SelectedItems.Count > 0)
{
string list1Remove = this.listBox1.SelectedItem.ToString();
//判斷是否添加到listbox2
if (!this.listBox2.Items.Contains(list1Remove))
{
//添加人員到listbox2中
this.listBox2.Items.Add(list1Remove);
//移除listbox1中
this.listBox1.Items.Remove(list1Remove);
}
}
}

//實現"<"功能,將右邊一項移到左邊
private void button3_Click(object sender, EventArgs e)
{
if (this.listBox2.SelectedItems.Count > 0)
{
string list2Remove = this.listBox2.SelectedItem.ToString();
//判斷是否添加到listbox2
if (!this.listBox1.Items.Contains(list2Remove))
{
//添加人員到listbox2中
this.listBox1.Items.Add(list2Remove);
//移除listbox1中
this.listBox2.Items.Remove(list2Remove);
}
}
}

//實現">>"功能,將左邊所有項移到右邊
private void button2_Click(object sender, EventArgs e)
{
listBox2.Items.AddRange(listBox1.Items);
listBox1.Items.Clear();
}

//實現"<<"功能,將右邊所有項移到左邊
private void button4_Click(object sender, EventArgs e)
{
listBox1.Items.AddRange(listBox2.Items);
listBox2.Items.Clear();
}


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

//將文本添加到左邊listBox中
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
listBox1.Items.Add(textBox1.Text.ToString());
}

}
//刪除左邊listBox某一項
private void button6_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
if (textBox1.Text == listBox1.Items[i].ToString())
{
listBox1.Items.RemoveAt(i);
i--;
}
}
}
}
//刪除左邊listBox所有項
private void button7_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}
}

}

 

 


 

 

實現的代碼二:

 

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 LisBox控件的操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.AddRange(listBox2.Items);
listBox2.Items.Clear();

}

private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}


//實現>功能
private void button1_Click(object sender, EventArgs e)
{
//獲取listbox1里面的值
if (this.listBox1.SelectedItems.Count > 0)
{
string checkPeople = this.listBox1.SelectedItem.ToString();
if (!this.listBox2.Items.Contains(checkPeople))
{
//添加人員到listbox2中
this.listBox2.Items.Add(checkPeople);
//移除listbox1中
this.listBox1.Items.Remove(checkPeople);
}
}
else {
MessageBox.Show("該人員已經轉移過");
}

}


//實現<功能
private void button7_Click(object sender, EventArgs e)
{
//獲取listbox2里面的值
if (this.listBox2.SelectedItems.Count > 0)
{
string checkPeople = this.listBox2.SelectedItem.ToString();
if (!this.listBox1.Items.Contains(checkPeople))
{
//添加人員到listbox1中
this.listBox1.Items.Add(checkPeople);
//移除listbox2中
this.listBox2.Items.Remove(checkPeople);
}
}
else
{
MessageBox.Show("該人員已經轉移過");
}
}

private void Form1_Load(object sender, EventArgs e)
{

}


//實現>>功能
private void button2_Click(object sender, EventArgs e)
{
listBox2.Items.AddRange(listBox1.Items);
listBox1.Items.Clear();
}


//刪除左邊的所有項
private void button6_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
}


//將文本加到左面的listBox
private void button4_Click(object sender, EventArgs e)
{
if (textBox1.Text != "") {
listBox1.Items.Add(textBox1.Text);
textBox1.Clear(); //清除textbox的里面的值
}
}

//刪除左面選擇的一項
private void button5_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedItems.Count > 0)
{
this.listBox1.Items.RemoveAt(this.listBox1.SelectedItems.Count);
}
else {
MessageBox.Show("您還沒有選擇,請選擇一項進行操作");
}
}

private void toolStripButton1_Click(object sender, EventArgs e)
{

}

private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{

}
}
}

 


免責聲明!

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



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