ListBox控件
常用属性:Name,Items,DaTaSource,DisplayMember,ValueMember,MultColumn,ColumuWidth,SelectionMode,SelectedIndex,SelectedItem,SelectedIndices,SelectedItems
常用事件:SelectedIndexChanged 选择项更改时触发事件
items方法:用于添加对象的:Add,Insert,AddRange;用于删除对象的:Remove,RemoveAt,Clear,以及用于查找的:Contains,IndexOf
知识点1:
MultColumn,ColumuWidth,组合使用,
MultColumn用于设置ListBox可以多列显示,ColumuWidth用于控制当LIst Box多列显示时列的宽度;
知识点2:
SelectionMode,选择模式控制,指示ListBox是单选,多选还是不可选模式;
SelectionMode=None;不可选
SelectionMode=One;单选
SelectionMode=MultiSimple;多选,通过鼠标单击实现多选
SelectionMode=MultiExtended;多选,可以通过是shift快捷键实现多选
知识点3:
数据填充,需要时使用DaTaSource,DisplayMember,ValueMember,使用方法如下:
private void FormListBox_Load(object sender, EventArgs e) { //创建一个DataTable DataTable dt = new DataTable("table1"); dt.Columns.Add("id", typeof(int)); dt.Columns.Add("name", typeof(string)); for (int i = 0; i < 20; i++) { DataRow dr = dt.NewRow(); dr["id"] = 1; dr["name"] = "name" + i; dt.Rows.Add(dr); dt.AcceptChanges(); } //将DataTable显示在ListBox中 listbox2.DisplayMember = "name"; listbox2.ValueMember = "id"; listbox2.DataSource = dt; }
知识点3
实现如下结果:将listbox2中选中的项,显示到右边的textbox中;
实现方法1:使用listbox的SelectedIndexChanged事件
List<string> listSles = new List<string>(); private void listbox2_SelectedIndexChanged(object sender, EventArgs e) { foreach (var item in listbox2.SelectedItems) { string str = (item as DataRowView).Row["name"].ToString(); if (!listSles.Contains(str)) { listSles.Add(str); } } textBox1.Text = string.Join(",", listSles); }
实现方法2:使用按钮提交事件
private void btnSubmit_Click(object sender, EventArgs e) { listSles = new List<string>(); foreach (var item in listbox2.SelectedItems) { string str = (item as DataRowView).Row["name"].ToString(); if (!listSles.Contains(str)) { listSles.Add(str); } } textBox1.Text = string.Join(",", listSles); }
知识点4:
listbox控件中item的增、删操作
private void button1_Click(object sender, EventArgs e) { //ListBox的索引从0开始 listBox1.Items.Add("学生");//增加一个 listBox1.Items.AddRange(new object[] { 222, 333, "aaa" });//增加一组 listBox1.Items.Insert(2, "老师");//插入一个 } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Remove(222);//移除 listBox1.Items.RemoveAt(2);//通过指定索引移除 listBox1.Items.Clear();//移除全部 }
知识点5:
两个listbox之间的左移和右移
//右移
private void btnRight_Click(object sender, EventArgs e) { if (listBoxLeft.SelectedItems!=null) { var list = listBoxLeft.SelectedItems; foreach (var item in list) { listBoxRight.Items.Add(item); } foreach (var item in listBoxRight.Items) { listBoxLeft.Items.Remove(item); } } } //左移 private void btnLeft_Click(object sender, EventArgs e) { var list = listBoxRight.SelectedItems; if (listBoxRight.SelectedItems != null) { foreach (var item in list) { listBoxLeft.Items.Add(item); } foreach (var item in listBoxLeft.Items) { listBoxRight.Items.Remove(item); } } } //全部右移 private void btnRightMoveAll_Click(object sender, EventArgs e) { listBoxRight.Items.AddRange(listBoxLeft.Items); listBoxLeft.Items.Clear(); } //全部左移 private void btnLeftMoveAll_Click(object sender, EventArgs e) { listBoxLeft.Items.AddRange(listBoxRight.Items); listBoxRight.Items.Clear(); }