按代码突出显示列表视图项背景色
本文关键字:视图 背景色 列表 显示 代码 | 更新日期: 2023-09-27 18:35:26
如果我通过鼠标单击列表视图的项目,颜色会变成"突出显示"颜色,但是如果我通过这样的代码执行此操作: (多选应该是真的,我也把隐藏选择设置为假)
myListView1.Items[2].Selected = true;
那么它将是灰色的...坏!我希望当我通过鼠标手动单击它们时它是相同的突出显示颜色:(
我也尝试添加此代码,但这也不起作用,仍然是灰色的
myListView1.Items[2].BackColor = System.Drawing.Color.Blue;
这是 ListView 在选定项但未聚焦时的行为。
因此,要获得您所追求的"蓝色"颜色,只需添加此颜色即可;
listView1.Focus();
您可以在列表视图的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
}
我在事件中使用的此代码略有不同(更复杂item_checked用于我的列表视图..希望它对您有所帮助..