ListView控件演示06:如何实现用户同时选择多个列表项

代码示例演示一个允许选择多项的ListView。
其实也就是把ListView.MultiSelect属性值设置为true,这样便能够同时选择多个列表项了。相反,则一次只能选择一个。默认属性值为true。

 

该示例演示如何设置HideSelection和HeaderStyle属性。
它还演示了ColumnHeader..::.Text、ColumnHeader..::.TextAlign和ColumnHeader..::.Width属性。

ListView控件演示06:如何实现用户同时选择多个列表项_第1张图片

 

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; namespace Demo06 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { this.InitializeListView(); } private void InitializeListView() { // 允许用户在列表中同时选择多个项。 this.listView.MultiSelect = true; ColumnHeader ColumnHeader1 = new ColumnHeader(); ColumnHeader1.Text = "Breakfast Item"; ColumnHeader1.TextAlign = HorizontalAlignment.Left; ColumnHeader1.Width = 142; ColumnHeader ColumnHeader2 = new ColumnHeader(); ColumnHeader2.Text = "Price Each"; ColumnHeader2.TextAlign = HorizontalAlignment.Center; ColumnHeader2.Width = 142; this.listView.Columns.Add(ColumnHeader1); this.listView.Columns.Add(ColumnHeader2); string[] FoodList = new string[] { "Juice", "Coffee", "Cereal & Milk", "Fruit Plate", "Toast & Jelly", "Bagel & Cream Cheese"}; string[] FoodPrice = new string[]{"1.09", "1.09", "2.19", "2.49", "1.49", "1.49"}; this.listView.BeginUpdate(); for (int Index = 0; Index < FoodList.Length; Index++) { ListViewItem Item = new ListViewItem(); Item.Text = FoodList[Index]; Item.SubItems.Add(FoodPrice[Index]); this.listView.Items.Add(Item); } this.listView.EndUpdate(); } } }

 

你可能感兴趣的:(ListView,String,object,Class,each)