【发布时间】:2013-08-27 13:45:28
【问题描述】:
编辑:我是个假人
所以...原来问题出在下面一行;
item.UseItemStyleForSubItems = false;
我从另一段代码中复制了这一行,我只是想更改一个SubItem,然后我改变主意并决定在这里我想更改整行。我的listView 有几个隐藏列,当UseItemStyleForSubItems 设置为false 时,它只会更改第一个SubItem。所以这种变化可能一直在发生,而不是在整个行中发生。
这是grayOut 现在的样子:
internal static void grayOut(ref ListView myLV)
{
//change each selected item to gray text
//currently, multiselect is turned off, so this will only be one item at a time
foreach (ListViewItem item in myLV.SelectedItems)
{
item.Selected = false;
item.ForeColor = Color.Gray;
item.BackColor = Color.Gainsboro;
item.Font = new Font("MS Sans Serif", 8, FontStyle.Italic);
}
}
这和我想的一样简单。 :)
原始问题
我正在使用以下代码更改已对其执行选定操作的项目的ForeColor。
public partial claass MyForm: Form
private void bgProgress_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Global.grayOut(ref this.lvUsers);
}
...
public static class Global
internal static void grayOut(ref ListView myLV)
{
//change each selected item to gray text
//currently, multiselect is turned off, so this will only be one item at a time
foreach (ListViewItem item in myLV.SelectedItems)
{
item.UseItemStyleForSubItems = false;
item.ForeColor = Color.Gray;
item.Font = new Font("MS Sans Serif", 10, FontStyle.Italic);
item.Selected = false;
}
myLV.Refresh();
}
我有两个问题。
- 属性更改,但该更改未显示。换句话说,我知道
ForeColor已更改为灰色,因为稍后我会在用户尝试执行某个操作时检查它是否为灰色。但是,它不会显示灰色或斜体。 -
我还使用以下方法尝试取消
MouseDown事件以防止再次选择该项目,但最终仍会被选中:private void lvUsers_MouseDown(object sender, MouseEventArgs e) { // Make sure it was a single left click, like the normal Click event if (e.Button == MouseButtons.Left) { ListViewHitTestInfo htInfo = lvUsers.HitTest(e.X, e.Y); if (htInfo.Item.ForeColor == Color.Gray) { return; } } }
我还没有找到任何其他方法来取消MouseDown 事件,所以我不确定还有什么可以尝试的。
【问题讨论】:
-
你在哪里打电话给
grayOut? -
来自
private void bgProgress_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) -
输出中是否抛出异常?
-
所有选择的项目都有默认的
backcolor - blue和forecolor - white,你不能轻易改变。 -
@keyboardP,没有例外。
标签: c# listview colors mousedown