1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 usingSystem.Windows.Forms;10
11 namespaceWinApp02_ListBox12 {13 public partial classForm1 : Form14 {15 publicForm1()16 {17 InitializeComponent();18 }19 //
20 //加载窗口
21 private void Form1_Load(objectsender, EventArgs e)22 {23 //绑定列表数据
24 BindListData();25 }26 ///
27 ///绑定列表数据28 ///
29 private voidBindListData()30 {31 //绑定多个图书信息实例32 //List lstBook = new List()33 //{34 //new Book() { ID = 1, Title = "ASP.NET", Price = 36.50m } ,35 //new Book() { ID = 2, Title = "C#本质论", Price = 55.80m } ,36 //new Book() { ID = 3, Title = "CRL", Price = 87.00m } ,37 //new Book() { ID = 4, Title = "ADO.NET", Price = 42.50m } ,38 //new Book() { ID = 5, Title = "Python", Price = 38.40m }39 //};40
41 //将lstBook中的数据遍历出来并存储到listBox1中.42 //foreach (Book book in lstBook)43 //{44 // //自定义类型或非字符串类型,默认会显示其完全限定名(命名空间.类名: WinApp02_ListBox.Book)45 // //所以必须重写Book类的ToString()方法46 //listBox1.Items.Add(book);47 //}
48
49 List lstBook = new List();50 lstBook.Add(new Book() { ID = 1, Title = "ASP.NET", Price = 36.50m});51 lstBook.Add(new Book() { ID = 2, Title = "C#本质论", Price = 55.80m});52 lstBook.Add(new Book() { ID = 3, Title = "CRL", Price = 87.00m});53 lstBook.Add(new Book() { ID = 4, Title = "ADO.NET", Price = 42.50m});54 lstBook.Add(new Book() { ID = 5, Title = "Python", Price = 38.40m});55
56 //指定数据源一次性绑定数据(同样,必须重写ToString()方法,否则即显示【命名空间.类名】
57 listBox1.DataSource =lstBook;58 //将对象属性作为列表显示的文字; 即Book.Title
59 listBox1.DisplayMember = "Title";60 //指定对象的值属性作为"值"字段,可以用ID或者Price;
61 listBox1.ValueMember = "ID";62 //listBox1.ValueMember = "Price";
63
64 }65
66 private void listBox1_SelectedIndexChanged(objectsender, EventArgs e)67 {68 //GetSelectedInfo_Foreach();
69 GetSelectedInfo_Linq();70
71 }72 ///获取选中的信息, foreach写法73 ///
74 private voidGetSelectedInfo_Foreach()75 {76 if (-1 ==listBox1.SelectedIndex)77 {78 MessageBox.Show("当前未选中任何项");79 return;80 }81
82 //获取当前所有选中项的索引
83 string index = "";84 foreach(int i inlistBox1.SelectedIndices)85 {86 index += i.ToString()+",";87 }88
89 //获取当前所有选中项的ID,Title,Price
90 string ids = "";91 string titles = "";92 string prices = "";93 foreach (Book book inlistBox1.SelectedItems)94 {95 ids += book.ID + ",";96 titles += book.Title + ",";97 prices += book.Price + ",";98 }99 label1.Text = "当前选中项的索引:" + index; //+ listBox1.SelectedIndex.ToString();
100 label2.Text = "选中的图书ID分别为:" +ids;101 label3.Text = "选中的图书名称分别为:" +titles;102 label4.Text = "选中的图书价格分别为:" +prices;103 }104
105 ///GetSelectedInfo_Linq: 获取选中的信息, Linq写法106 ///107 private voidGetSelectedInfo_Linq()108 {109 if ( -1 ==listBox1.SelectedIndex )110 {111 MessageBox.Show("当前未选中任何项");112 return;113 }114
115 string index = "";116 foreach (int i inlistBox1.SelectedIndices)117 {118 index += i.ToString() + ",";119 }120 //Linq写法
121 label1.Text = "当前选中项的索引:" +index;122 label2.Text = "选中的图书ID分别为:"+ string.Join(",", listBox1.SelectedItems.Cast().Select(book =>book.ID).ToArray());123 label3.Text = "选中的图书名称分别为:" + string.Join(",",listBox1.SelectedItems.Cast().Select(book =>book.Title).ToArray());124 label4.Text = "选中的图书价格分别为:" + string.Join(",", listBox1.SelectedItems.Cast().Select(book =>book.Price).ToArray());125 }126
127 }128 }