【问题标题】:Highlight multiple items/rows in listBox突出显示列表框中的多个项目/行
【发布时间】:2012-11-18 05:10:21
【问题描述】:

我有一个列表框,每行显示 (X,Y) 的一些位置。

不知何故,用户可以在文本框中输入几个 (X,Y) 对,然后按下按钮。

现在我想要做的是:每次用户输入 3 或 4 个 (X,Y) 对时,我的算法都会找到匹配的对,并且那些对应的对应该被突出显示(比如说粉红色/红色/任何颜色)同时在列表框中。

如何用我想要的颜色突出显示这些对(相同的索引)?


第一版:

按照NikolaD - Nick 的指导,我将 DrawMode 更改为 OwnerDrawVariable 并在 lsBoxFeature_DrawItem 方法中添加了以下代码:

private void lsBoxFeature_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawFocusRectangle();
        Bitmap bmp = new Bitmap(e.Bounds.Width, e.Bounds.Height);
        Graphics g = Graphics.FromImage(bmp);


            foreach (var item in globalDataForAllMatchedFrames[globalDataForAllMatchedFrames.Count - 1].featureNumber)
            {
                if (lsBoxFeature.Items[e.Index].Equals(item))//your method that determines should current item be highlighted 
                {
                    g.Clear(Color.Red);
                }
                else
                {
                    g.Clear(lsBoxFeature.BackColor);
                }

                g.DrawString(lsBoxFeature.Items[e.Index].ToString(), lsBoxFeature.Font, new SolidBrush(lsBoxFeature.ForeColor), e.Bounds);
                e.Graphics.DrawImage(bmp, e.Bounds);
                g.Dispose();
            }

    }

item 是一个 PointF 对象,现在每次 item 等于 listBoxFeature 中的那些成员时,它应该以红色突出显示它们。

有两个问题:

I) 似乎方法 .Equals 在 if 条件下无法正常工作以检查 pointF 项是否等于 listBoxFeature ===> 中的成员,因此我的 listBoxFeature 中没有显示任何内容

II) 即使我运行代码,我也会收到如下错误消息:


第二版:

我听从了NikolaD - Nick 的建议,它成功了!!!。但是有一小部分需要解决,它没有显示 lsBoxFeature 中每一行的文本(PointF 坐标)。

这是现在的样子:

这是输出应该是这样的:

如何在 lsBoxFeature 中取回该行的 tex?

【问题讨论】:

  • 看看这个link 一次。也许对你有帮助。
  • @Mr_Green:这是一个列表框,不是列表视图
  • 哎呀,你是对的......你有没有解决这个问题?
  • @farzinparsa 编辑了一点代码,检查答案的评论。只需再次复制g.DrawString
  • @NikolaD-Nick:在我再次复制 g.DrawString 行之后,甚至红色都消失了,它只显示文本(正常,没有任何突出显示)

标签: c# .net c#-4.0 listbox


【解决方案1】:

您应该添加ListViewDrawItem 事件处理程序,并在检查哪个Items 应该着色时突出显示。像这样的:

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
                e.DrawFocusRectangle();
                Bitmap bmp = new Bitmap(e.Bounds.Width, e.Bounds.Height);
                Graphics g = Graphics.FromImage(bmp);

                if (MeetsCriterion(listBox1.Items[e.Index]))//your method that determines should current item be highlighted 
                {
                    g.Clear(Color.Red);
                }
                else
                {
                    g.Clear(listBox1.BackColor);
                }
                g.DrawString(listBox1.Items[e.Index].ToString() , listBox1.Font, new SolidBrush(listBox1.ForeColor), e.Bounds);
                e.Graphics.DrawImage(bmp, e.Bounds);
                g.Dispose();
        }

检查这个问题,有一个更详细的答案如何做到这一点:How do i add same string to a ListBox in some lines?

**编辑:**此编辑是在您编辑了您的问题之后。对 listBox 中的每个项目调用 lsBoxFeature_DrawItem 事件处理程序,而不是对所有项目调用一次。第一个问题是为对象调用 Equals() 方法(ListBox 中的项是对象)有效地比较其他对象的引用,而不是 PointF 的值。第二个问题是您处理了 Graphic 对象,然后调用了 g.Clear()在处置的对象上。我已经重写了你的代码,我认为它现在可以工作了。

private void lsBoxFeature_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawFocusRectangle();
            Bitmap bmp = new Bitmap(e.Bounds.Width, e.Bounds.Height);
            Graphics g = Graphics.FromImage(bmp);

            bool found = false;
            int count = 0;
            PointF pF1 = (PointF)lsBoxFeature.Items[e.Index];
            while (!found && count < globalDataForAllMatchedFrames[globalDataForAllMatchedFrames.Count - 1].featureNumber.Count)
            {
                //next two lines are here to show you the problem with equals!!!!

                PointF pF2 = (PointF)globalDataForAllMatchedFrames[globalDataForAllMatchedFrames.Count - 1].featureNumber[count];
                if(pF1.Equals(pF2))
                {
                    found = true;
                }
                count++;
            }

            if (found)//your method that determines should current item be highlighted 
            {
                g.Clear(Color.Red);
            }
            else
            {
                g.Clear(lsBoxFeature.BackColor);
            }
            g.DrawString(lsBoxFeature.Items[e.Index].ToString(), lsBoxFeature.Font, new SolidBrush(lsBoxFeature.ForeColor),  new Rectangle(e.Bounds.X,0,e.Bounds.Width,e.Bounds.Height));
            e.Graphics.DrawImage(bmp, e.Bounds);
            g.Dispose();

        } 

【讨论】:

  • 它在你的第一版之后就开始工作了。唯一的问题是:它没有用于显示每一行的文本[因为我在我的答案中包含了新版(第 2 版)中的图像],并且那些预期的行只是用红色突出显示但没有文本。但是现在在我再次复制 g.DrawString 行之后,甚至红色都消失了,它只显示文本(正常没有任何突出显示)
  • 这一定行得通,你确定你只复制了这一行g.DrawString(lsBoxFeature.Items[e.Index].ToString(), lsBoxFeature.Font, new SolidBrush(lsBoxFeature.ForeColor), new Rectangle(e.Bounds.X,0,e.Bounds.Width,e.Bounds.Height));
  • 是的,那是我唯一更改的行,但现在所有红色高亮都没有显示。它只显示简单的行(仅文本)
  • 不是代码的问题,是 listBox 和 featureNumber 列表中的项目不匹配,你确定,你做的一切都和之前一样吗?
  • 我的错!抱歉,我忘记再次将 DrawMode 更改为 OwnerDrawVariable。它正在工作。
猜你喜欢
  • 2013-06-12
  • 2015-12-21
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多