【发布时间】: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 行之后,甚至红色都消失了,它只显示文本(正常,没有任何突出显示)