2012-11-17 75 views
0

我有一个列表框,显示每行中(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(); 
      } 

    } 

项是一个对象,其是一个PointF,现在每当该项目与listBoxFeature中的成员相等时,它应该用红色突出显示它们。

这里有两个问题:

I)似乎methos .Equals正确犯规中,如果条件检查工作,如果该项目的PointF在listBoxFeature ===>如图所示的结果没什么等于成员在我listBoxFeature

II),甚至当我运行代码我得到一个错误信息如下:

enter image description here


第2版:

我也跟着NikolaD - Nick建议,它的工作!但有一小片来解决,它不显示在lsBoxFeature每一行文字(的PointF坐标)。

这是现在的样子:

enter image description here

,这里是输出是如何应该是:

enter image description here

我怎么能走行的TEX早在lsBoxFeature ?

+0

看看这个['link'](http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.hittest.aspx)一次。也许这对你有帮助。 –

+0

@Mr_Green:它是一个列表框,而不是列表视图 –

+0

oops是的,你是对的..你有解决这个问题的方法吗? –

回答

3

您应该添加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中的Item是对象),它有效地比较了其他对象的引用,而不是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(); 

     } 
+0

在' g.DrawString'行,我编辑它!抱歉。 –

+0

它已经在第一版后开始工作。唯一的问题是:它没有用来显示每行的文本[因为我在我的答案中包含了新编辑(第2版)中的图像],并且那些预期的行只是用红色突出显示,但没有文本。 但现在我复制g.DrawString行后,即使红色已经消失,它只是显示文本(正常,没有任何突出显示) –

+0

这必须工作,,你确定你只复制这行'g。 DrawString(lsBoxFeature.Items [e.Index] .ToString(),lsBoxFeature.Font,新的SolidBrush(lsBoxFeature.ForeColor),新的Rectangle(e.Bounds.X,0,e.Bounds.Width,e.Bounds.Height) );' –

相关问题