ListView控件(在ListView失去焦点的情况下仍然保持Item高亮)
添加列:
在“属性”窗口中,将该控件的 View 属性设置为 Details。
在“属性”窗口中,单击 Columns 属性旁的“省略号”按钮 ()。
出现“ColumnHeader 集合编辑器”。
使用“添加”按钮添加新列。然后可以选择列标题并设置其文本(列的标题文字)、文本对齐方式和宽度。
代码
private
void
button2_Click(
object
sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0 )
MessageBox.Show(listView1.SelectedItems[ 0 ].Text);
else
MessageBox.Show( " 请选择 " );
}
private void ListviewDemo_Load( object sender, EventArgs e)
{
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
}
private void toolStripMenuItem1_Click( object sender, EventArgs e)
{
listView1.Items.Add( new ListViewItem( new string [] { " 添加 " , " 添加 " }));
}
private void toolStripMenuItem2_Click( object sender, EventArgs e)
{
MessageBox.Show( " 删除 " + listView1.SelectedItems[ 0 ].Text);
listView1.SelectedItems[ 0 ].Remove();
}
#region 在ListView失去焦点的情况下仍然保持Item高亮
private void listView_Validated( object sender, EventArgs e)
{
if (listView1.FocusedItem != null )
{
listView1.FocusedItem.BackColor = SystemColors.Highlight;
listView1.FocusedItem.ForeColor = Color.White;
listView1.SelectedIndices.Add(listView1.FocusedItem.Index); // **真正让其选择
}
}
private void listView_ItemSelectionChanged( object sender, ListViewItemSelectionChangedEventArgs e)
{
e.Item.ForeColor = Color.Black;
e.Item.BackColor = SystemColors.Window;
if (listView1.FocusedItem != null )
{
listView1.FocusedItem.Selected = true ;
}
}
#endregion
{
if (listView1.SelectedItems.Count > 0 )
MessageBox.Show(listView1.SelectedItems[ 0 ].Text);
else
MessageBox.Show( " 请选择 " );
}
private void ListviewDemo_Load( object sender, EventArgs e)
{
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
listView1.Items.Add( new ListViewItem( new string [] { " A " , " a " }));
listView1.Items.Add( new ListViewItem( new string [] { " B " , " b " }));
listView1.Items.Add( new ListViewItem( new string [] { " C " , " c " }));
listView1.Items.Add( new ListViewItem( new string [] { " D " , " d " }));
}
private void toolStripMenuItem1_Click( object sender, EventArgs e)
{
listView1.Items.Add( new ListViewItem( new string [] { " 添加 " , " 添加 " }));
}
private void toolStripMenuItem2_Click( object sender, EventArgs e)
{
MessageBox.Show( " 删除 " + listView1.SelectedItems[ 0 ].Text);
listView1.SelectedItems[ 0 ].Remove();
}
#region 在ListView失去焦点的情况下仍然保持Item高亮
private void listView_Validated( object sender, EventArgs e)
{
if (listView1.FocusedItem != null )
{
listView1.FocusedItem.BackColor = SystemColors.Highlight;
listView1.FocusedItem.ForeColor = Color.White;
listView1.SelectedIndices.Add(listView1.FocusedItem.Index); // **真正让其选择
}
}
private void listView_ItemSelectionChanged( object sender, ListViewItemSelectionChangedEventArgs e)
{
e.Item.ForeColor = Color.Black;
e.Item.BackColor = SystemColors.Window;
if (listView1.FocusedItem != null )
{
listView1.FocusedItem.Selected = true ;
}
}
#endregion