C# 突出显示列表框中的多个项目/行
我有一个列表框,显示每行中(X,Y)的一些位置 用户可以在文本框中输入几个(X,Y)对,然后按下按钮 现在我想做的是:每次用户输入3或4(X,Y)对时,我的算法都会找到匹配的对,这些对应的对应该同时高亮显示(比如粉色/红色/任何颜色),所有这些都在列表框中 我如何用我想要的颜色突出显示这些对(相同的索引)C# 突出显示列表框中的多个项目/行,c#,.net,c#-4.0,listbox,C#,.net,C# 4.0,Listbox,我有一个列表框,显示每行中(X,Y)的一些位置 用户可以在文本框中输入几个(X,Y)对,然后按下按钮 现在我想做的是:每次用户输入3或4(X,Y)对时,我的算法都会找到匹配的对,这些对应的对应该同时高亮显示(比如粉色/红色/任何颜色),所有这些都在列表框中 我如何用我想要的颜色突出显示这些对(相同的索引) 第一版: 根据指导,我将DrawMode更改为OwnerDrawVariable,并在lsBoxFeature_DrawItem方法中添加了以下代码: private void lsBoxF
第一版: 根据指导,我将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中的那些成员时,它都应该以红色突出显示它们
有两个问题:
一) 在if条件下检查pointF项是否等于listBoxFeature===>中的成员,Methodos.Equals似乎无法正常工作,因此我的listBoxFeature中没有显示任何内容
二) 即使在运行代码时,也会收到如下错误消息:
第二版: 我听从了他的建议,结果成功了!!!。但是有一个小问题需要解决,它不显示lsBoxFeature中每一行的文本(PointF坐标) 下面是它现在的样子: 下面是输出应该是怎样的:
如何在lsBoxFeature中恢复行的tex?您应该添加
ListView
的DrawItem
事件处理程序,并在检查哪些项应上色后突出显示。大概是这样的:
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();
}
检查此问题,这里有一个更详细的答案,您可以如何做到这一点:
**编辑:**此编辑是在您编辑问题之后进行的。为listBox中的每个项调用lsBoxFeature\u DrawItem事件处理程序,而不是为所有项调用一次。第一个问题是为object调用Equals()方法(ListBox中的项是object),有效地比较了其他对象的引用,而不是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();
}
private void lsBoxFeature\u DrawItem(对象发送方,DrawItemEventArgs e)
{
e、 DrawFocusRectangle();
位图bmp=新位图(e.Bounds.Width,e.Bounds.Height);
Graphics g=Graphics.FromImage(bmp);
bool-found=false;
整数计数=0;
PointF pF1=(PointF)lsBoxFeature.Items[e.Index];
而(!found&&count
您应该添加列表视图
的DrawItem
事件处理程序,并在检查哪些项目
应该上色时突出显示。大概是这样的:
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();
}
检查此问题,这里有一个更详细的答案,您可以如何做到这一点:
**编辑:**此编辑是在您编辑问题之后进行的。为listBox中的每个项调用lsBoxFeature\u DrawItem事件处理程序,而不是为所有项调用一次。第一个问题是为object调用Equals()方法(ListBox中的项是object),有效地比较了其他对象的引用,而不是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();
}
private void lsBoxFeature\u DrawItem(对象发送方,DrawItemEventArgs e)
{
e、 DrawFocusRectangle();
位图bmp=新位图(e.Bounds.Width,e.Bounds.Height);
Graphics g=Graphics.FromImage(bmp);
bool-found=false;
整数计数=0;
PointF pF1=(PointF)lsBoxFeature.Items[e.Index];
而(!found&&count
看一下这个。也许这对你有帮助。@U!Green先生:这是一个列表框,而不是列表视图。哦,是的,你是对的。。“你找到解决这个问题的办法了吗?”法津帕萨编辑了一下