想在ADD得时候 如果Memo1.Lines.Add('正常')得时候 这个正常就是绿色 
如果Memo1.Lines.Add('不正常')得时候 这个正常就是红色

解决方案 »

  1.   

    Memo不支持每行颜色的设定,改用RichEdit,SelAttributes属性...
      

  2.   

    就算我用 RichEdit
    那么 怎么指定第 i 行得颜色?
      

  3.   

    使用ListBox, Style 设为 lbOwnerDrawFixed
    OnDrawItem 自己画
    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    var
      s: String;
    begin
      s := ListBox1.Items.Strings[Index];
      if s = '正常' then
        ListBox1.Canvas.Font.Color := clGreen
      else if s = '不正常' then
        ListBox1.Canvas.Font.Color := clRed
      else
        ListBox1.Canvas.Font.Color := clBlack;
      ListBox1.Canvas.TextRect(Rect, Rect.Left, Rect.Top, ListBox1.Items.Strings[Index]);
    end;
      

  4.   

    如果用RichEdit,可以这样:
    with RichEdit1 do
       begin
          SelAttributes.Color:=clGreen;
          Lines.Add('正常');
          SelAttributes.Color:=clRed;
          Lines.Add('不正常');
       end;
      

  5.   

    先别管正常 不正常 我就就想:  比如 我就想指定 RichEdit 的第5行 颜色是 clred怎做?
      

  6.   

    procedure CrnSetRichEditLineColor(re: TRichEdit; cl: TColor; nRow: Integer);
    var
        nStartPos, nEndPos: integer;
    begin
        if nRow < 0 then
            nRow := 0;
        if nRow > re.Lines.Count - 1 then
        begin
            nRow := re.Lines.Count - 1;
            nStartPos := re.Perform(EM_LINEINDEX, nRow, 0);
            nEndPos := Length(re.Lines.Text);
        end
        else
        begin
            nStartPos := re.Perform(EM_LINEINDEX, nRow, 0);
            nEndPos := re.Perform(EM_LINEINDEX, nRow + 1, 0);
        end;    re.SelStart := nStartPos;
        re.SelLength := nEndPos - nStartPos;
        re.SelAttributes.Color := cl;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
        CrnSetRichEditLineColor(RichEdit1, clRed, 4);
    end;