列表框项目颜色在错误的地方

时间:2015-11-25 18:25:39

标签: c#

我想给每组文件名一个不同的颜色, 但只有第一个文件名在此代码中具有不同的颜色, 调试代码时我找不到任何错误, 可能是什么问题?...

public class ListBoxEx : ListBox
 {
  private string PreviousText = string.Empty;
  private string CurrentText = string.Empty;
  private bool equal = false;

  public ListBoxEx()
  {
     this.DrawMode = DrawMode.OwnerDrawFixed;
  }

  protected override void OnDrawItem(DrawItemEventArgs e)
  {
   try
   {
    e.DrawBackground();

    if (this.Items.Count > 0 && e.Index > 0)
    {
      PreviousText = Path.GetFileName(this.Items[e.Index - 1].ToString());
      CurrentText = Path.GetFileName(this.Items[e.Index].ToString());

      if (CurrentText == PreviousText)
       equal = true;
      else equal = false;

      if (equal)
       e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow), e.Bounds);
      else
       e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), e.Bounds);

      e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), new PointF(e.Bounds.X, e.Bounds.Y));

  base.OnDrawItem(e);
    }
   }
   catch (Exception ex)
    {
    throw (ex);
   }
  }
 }

1 个答案:

答案 0 :(得分:0)

找到它,

$outfile