Winform中两个listbox的操作是平时比较常用的操作。
本次将以一个Winform实例来分享一下两个listbox的操作,包括:listbox添加项,项的上移下移等操作。
假设有两个listbox:一个叫listBoxCategory,这个listbox是一个包含所有建筑元素项目的项;另一个叫listBoxSelected,这个是将左边的listbox中的项选中并添加到的listbox。
<1> Resource文件的定义:
<2> listBox中的每一项定义:
public class CategoryItem { public CategoryItem(string categoryName, int categoryId) { CategoryName = categoryName; CategoryId = categoryId; } public string CategoryName {get; set;} public int CategoryId {get; set;} }
<3> listBoxCategoery在初始化时,它的每一项都是由doc.Settings.Categories提供,但是不能和listBoxSelected中的初始项冲突;listBoxSelected中的初始项由Existinglist提供
private void listboxInitialize(Document doc) { List<CategoryItem> categoryitems = new List<CategoryItem>(); listBoxCategory.DisplayMember = "CategoryName"; listBoxCategory.ValueMember = "id"; listBoxSelected.DisplayMember = "CategoryName"; listBoxSelected.ValueMember = "id"; foreach (Category cat in doc.Settings.Categories) { bool checkreapter = false; foreach(int defaultlistId in Existinglist) { if(cat.Id.IntegerValue==defaultlistId) { checkreapter = true; break; } } if(checkreapter == false) { listBoxCategory.Items.Add(new CategoryItem(cat.Name, cat.Id.IntegerValue)); //防止和listBoxSelected中的项冲突 } } foreach (int defaultlistId in Existinglist) { listBoxSelected.Items.Add(new CategoryItem(doc.Settings.Categories.get_Item((BuiltInCategory)defaultlistId).Name, defaultlistId)); } }
<4> listBoxSeleted的上移与下移:
private void buttonUp_Click(object sender, EventArgs e) { int listboxLength = listBoxSelected.Items.Count; int selectindex = listBoxSelected.SelectedIndex; if (listBoxSelected.SelectedItem !=null) { if (listboxLength > selectindex && selectindex > 0) { object oTempItem = listBoxSelected.SelectedItem; listBoxSelected.Items.RemoveAt(selectindex); listBoxSelected.Items.Insert(selectindex - 1, oTempItem); listBoxSelected.SelectedIndex = selectindex - 1; } } else { MessageBox.Show(Res.StringReminder); } } private void buttonDown_Click(object sender, EventArgs e) { int listboxLength = listBoxSelected.Items.Count; int selectindex = listBoxSelected.SelectedIndex; if (listBoxSelected.SelectedItem !=null) { if (listboxLength > selectindex && selectindex < listboxLength - 1) { object oTempItem = listBoxSelected.SelectedItem; listBoxSelected.Items.RemoveAt(selectindex); listBoxSelected.Items.Insert(selectindex + 1, oTempItem); listBoxSelected.SelectedIndex = selectindex + 1; } } else { MessageBox.Show(Res.StringReminder); } }
<5> 将listBoxCategory中的项添加到listBoxSelected:
private void buttonAdd_Click(object sender, EventArgs e) { listBoxSelected.DisplayMember = "CategoryName"; listBoxSelected.ValueMember = "id"; bool checklist = false; if (listBoxCategory.SelectedItem !=null) { foreach(object obj in listBoxSelected.Items) { if (((CategoryItem)listBoxCategory.SelectedItem).CategoryName == ((CategoryItem)obj).CategoryName) { checklist = true; break; } } if(checklist==true) { MessageBox.Show(Res.StringRepeat); } else { for (int i = listBoxCategory.SelectedItems.Count - 1; i >= 0; i--) { listBoxCategory.SelectedItem = listBoxCategory.SelectedItems[i]; listBoxSelected.Items.Add(listBoxCategory.SelectedItem); listBoxCategory.Items.Remove(listBoxCategory.SelectedItem); } } } else { MessageBox.Show(Res.StringReminder); } }
<6> listBoxcategory中的项双击添加到listBoxSelected中:
private void listBoxCategory_DoubleClick(object sender, EventArgs e) { buttonAdd.PerformClick(); }