我想做一个上下文比较的功能,界面上有两个RichEdit 只有在下边的可以输入,与上边的RichEdit 作对照输入一个字母对应一个字母,但是需要输入过程中把输入错误的用红色标出来,这样的功能怎样实现,主要的问题是 RichEdit 只有选中的可以改变颜色,不选中的不能改变,虽然设置了SelAttributes 但是这时事前设置,我是需要输入之后根据输入的正确与否改变颜色,这个怎么实现

解决方案 »

  1.   

    uses Math; //use Min()procedure TForm1.Memo1Change(Sender: TObject);
    var
      S: WideString;
      T: WideString;
      I: Integer;
    begin
      RichEdit1.HideSelection := True;
      RichEdit1.Lines.BeginUpdate;
      RichEdit1.SelectAll;
      RichEdit1.SelAttributes.Color := clWindowText;
      RichEdit1.SelAttributes.Style := [];
      S := RichEdit1.Text;
      T := TEdit(Sender).Text;
      for I := 1 to Min(Length(T), Length(S)) do
      begin
        RichEdit1.SelStart := Length(string(Copy(S, 1, I - 1)));
        RichEdit1.SelLength := Length(S[I]);
        if T[I] = S[I] then
          RichEdit1.SelAttributes.Color := clGreen
        else
        begin
          RichEdit1.SelAttributes.Color := clRed;
          RichEdit1.SelAttributes.Style := [fsUnderline];
        end;
      end;
      RichEdit1.Lines.EndUpdate;
    end;