ListView控件的使用
//ListView 的设计
this.ListView1 = new System.Windows.Forms.ListView();
this.ListView1.BackColor = System.Drawing.SystemColors.Control;
this.ListView1.Dock = System.Windows.Forms.DockStyle.Top;
this.ListView1.Location = new System.Drawing.Point(0, 0);
this.ListView1.Name = "ListView1";
this.ListView1.Size = new System.Drawing.Size(292, 130);
this.ListView1.TabIndex = 0;
this.ListView1.View = System.Windows.Forms.View.Details;
this.ListView1.MultiSelect = true;
this.ListView1.HideSelection = false;
this.ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
ColumnHeader columnHeader1 = new ColumnHeader();
columnHeader1.Text = "Breakfast Item";
columnHeader1.TextAlign = HorizontalAlignment.Left;
columnHeader1.Width = 146;
ColumnHeader columnHeader2 = new ColumnHeader();
columnHeader2.Text = "Price Each";
columnHeader2.TextAlign = HorizontalAlignment.Center;
columnHeader2.Width = 142;
this.ListView1.Columns.Add(columnHeader1);
this.ListView1.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"};
for(int count=0; count < foodList.Length; count++)
{
ListViewItem listItem = new ListViewItem(foodList[count]);
listItem.SubItems.Add(foodPrice[count]);
ListView1.Items.Add(listItem);
}
this.Controls.Add(ListView1);
}
ListView 先触发LostFocus事件然后触发Leave事件,一下代码需要将HideSelection设置为true
void listView_LostFocus(object sender, EventArgs e)
{
ListView listView1 = sender as ListView;
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
if (listView.SelectedItems[i].Selected)
{
// MessageBox.Show(i.ToString());
listView1.SelectedItems[i].BackColor = System.Drawing.Color.CadetBlue;
}
}
this.listView.Refresh();
}
private void rfsdrsd_Click(object sender, EventArgs e)
{
}
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
//for (int i = 0; i < this.listView.SelectedItems.Count; i++)
//{
// this.listView.SelectedItems[i].BackColor = System.Drawing.Color.CadetBlue;
//}
}
private void listView_Leave(object sender, EventArgs e)
{
//for (int i = 0; i < this.listView.SelectedItems.Count; i++)
//{
// if (this.listView.SelectedItems[i].Selected)
// {
// //MessageBox.Show(i.ToString());
// this.listView.SelectedItems[i].BackColor = System.Drawing.Color.Yellow;
// }
//}
// this.listView.Refresh();
}
}