制作帶復選框的ListView控件
阿新 • • 發佈:2018-12-23
istview for 復選框 數據 網格線 items name 判斷 write
實現效果:
知識運用
ListView控件的GridLines //設置是否在ListView控件中顯示網格線
public bool GridLines{get;set}
和CheckBoxes屬性 //設置listView控件中各數據項是否顯示復選框
public bool CheckBoxes{get;set}
以及ListViewItem數據項的Checked屬性 //判斷是否選中此數據項中的復選框
public bool Checked{get;set}
實現代碼:
private void Form1_Load(object sender, EventArgs e) { listView1.CheckBoxes = true; listView1.GridLines = true; listView1.View = View.Details; listView1.Columns.Add("文件名稱",180,HorizontalAlignment.Left); listView1.Columns.Add("修改日期",217,HorizontalAlignment.Left); foreach (string fileName in Directory.GetFiles("C:\\windows\\")) { FileInfo file = new FileInfo(fileName); ListViewItem OptionItem = new ListViewItem(file.Name); OptionItem.SubItems.Add(file.LastWriteTime.ToString()); listView1.Items.Add(OptionItem); } } private void button1_Click(object sender, EventArgs e) { foreach (ListViewItem lvi in listView1.Items) { if(lvi.Checked==false) lvi.Checked = true; else lvi.Checked = false; } } private void button2_Click(object sender, EventArgs e) { listView1.Items.Clear(); }
制作帶復選框的ListView控件