如何让listbox中的每一行的字体颜色不同,例如,我要求listbox中的字体是一行蓝色的,一行红色的!

解决方案 »

  1.   

    procedure TForm.ListviewDrawItem(Control: TWinControl;
      Index: Integer; Rect: TRect; State: TOwnerDrawState);
    begin
        if odd(index) then
            ListBox1.Canvas.Font.Color:=clred
        else
            ListBox1.Canvas.Font.Color:=clblue;
        ListBox1.Canvas.TextRect(Rect,rect.Left,rect.Top,ListBox1.Items[Index]);
    end;
      

  2.   

    可以,用ListBox的画布功能,将Style设成lbOwnerDrawFixed 或者lbOwnerDrawVariable类型,在OnDrawItem事件里,利用事件内的Index,以不同颜色进行重画即可。
      

  3.   

    谢谢,但是我的listbox中的内容是动态产生的,例如我在显示是链接主机的内容是红色的,取消链接的时候显示的内容是蓝色的,又如何处理呢?
      

  4.   

    自己多想想!
    listbox的一个参考方法,返回items的序号:
    listbox.items.indexof(text);