按代码突出显示ListViewItem背景颜色

时间:2012-05-28 01:17:08

标签: c# winforms

如果我通过鼠标点击列表视图的项目,颜色将变为"突出显示"颜色,但如果我这样做的代码:  (MultiSelect应为True,我也将HideSelection设置为False)

myListView1.Items[2].Selected = true;
那么它将是灰色......糟糕!当我用鼠标手动点击它时,我希望它是相同的高亮颜色:(

我也尝试添加此代码,但这也没有用,仍为灰色

myListView1.Items[2].BackColor = System.Drawing.Color.Blue;

2 个答案:

答案 0 :(得分:1)

这是ListView选择项目时的行为,但重点。

所以,要获得你所追求的'蓝色'颜色,只需添加它;

listView1.Focus();

答案 1 :(得分:0)

你可以在listview的SelectedIndexChanged事件中试试吗?

ListViewItem lv = YourListview.GetItemAt(YourListView.PointToClient(Cursor.Position).X, YourListView.PointToClient(Cursor.Position).Y);

// this kind of Control.GetItemAt() works everywhere you need to find your mouse position ;)

// if you need to find position for screen (i.e. if you want to show a messagebox at the center of screen) you can use PointToScreen instead of PointToClient

 if (lv == null) 
 { 
   return; 
 }

else if (yourfirstpossibility == true)
{
  lv.Selected = true;
  lv.BackColor = Color.FromKnownColor(KnownColor.ButtonHighLight);

 // or which color you prefer. FromKnownColor retrieves system colors which you can see in backcolor / forecolor property => "system" named palette

}

这个代码有点不同(更复杂)我在list_checked事件中使用我的listview ..希望它可以帮助你..