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)
{
}
}
}