【问题标题】:ListViewItem.ForeColor Change Doesn't DisplayListViewItem.ForeColor 更改不显示
【发布时间】: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();
    }

我有两个问题。

  1. 属性更改,但该更改未显示。换句话说,我知道ForeColor 已更改为灰色,因为稍后我会在用户尝试执行某个操作时检查它是否为灰色。但是,它不会显示灰色或斜体。
  2. 我还使用以下方法尝试取消 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 - blueforecolor - white,你不能轻易改变。
  • @keyboardP,没有例外。

标签: c# listview colors mousedown


【解决方案1】:

您可以通过与SelectedIndexChanged Event 关联的方法来获得您想要的东西。示例代码:

private void myLV_SelectedIndexChanged(object sender, EventArgs e)
{
    if (myLV.SelectedItems.Count > 0)
    {
        foreach (ListViewItem item in myLV.SelectedItems)
        {
            if (item.ForeColor == Color.Gray)
            {
                item.Selected = false;
            }
            else
            {
                ListViewItem tempItem = item;
                grayOut2(ref tempItem);
            }
        }
    }
}

如果之前没有选择过,此代码会将您选择的任何项目变灰(未变灰);否则,它会避免它被选中。 GrayOut2 是仅考虑给定项目的函数版本。

internal static void grayOut2(ref ListViewItem item)
{
    //change each selected item to gray text
    //currently, multiselect is turned off, so this will only be one item at a time
    item.UseItemStyleForSubItems = false;
    item.ForeColor = Color.Gray;
    item.Font = new Font("MS Sans Serif", 10, FontStyle.Italic);
    item.Selected = false;
}

【讨论】:

  • 谢谢!这确实解决了选择问题。然而,grayOut 函数仍然没有改变显示。它改变了属性就好了。 ForeColor 是灰色的,但用户看不到它是灰色的。这可能与多线程有关吗?
  • @tmoore82 字体颜色应该立即更新。原则上,涉及 GUI 的多线程问题通常会输出错误;如果您使用正确的线程,应该没有任何问题。无法说明为什么颜色没有更新,但它肯定与您的程序有关。从头开始创建一个新项目,输入一个ListView和这段代码,你会看到它没有任何问题。
  • 谢谢,@varocarbas!我发现了问题并编辑了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 2016-11-11
  • 2013-01-12
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多