现有这么一个需求
输入10位密码,在输入前要能显示10位黑色的*号,每输入一位,一个*号变成红色
请教~

解决方案 »

  1.   


    用RichEdit实现比较简单。
      

  2.   

    如果单独用edit控件来实现的话只有用另一种方式来表示了,例如你在控件背景弄一个黑色*的图片.然后设置edit控件字体为红色.这样就可以了.
      

  3.   

    (*向楼主献上RichEdit实现的代码:代码长一点,但并不是因为使用了RichEdit,而是为了实现你要的功能*)var
      PswStr: string; //全局的变量,初值为''procedure TForm1.RichEdit1Enter(Sender: TObject);
    begin
      with TRichEdit(Sender) do begin
        Clear;
        Text := '**********';
        Tag := 0;
        SelStart := 0;
        SetFocus;
      end;
      PswStr := '';
    end;procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
      case Key of
        #1..#7, #9..#31 : ;
        #8 : with TRichEdit(Sender) do begin
                Tag := Tag - 1;
                if Tag < 0 then Tag := 0;
                SetLength(PswStr, Tag);
                SelStart := Tag;
                SelLength:= 10-Tag;
                SelAttributes.Color := clBlack;
                SelStart := Tag;
             end;
        else with TRichEdit(Sender) do
               if Tag <= 9 then begin
                 PswStr := PswStr + Key;
                 SelStart := Tag;
                 SelLength:= 1;
                 Tag := Tag + 1;
                 SelAttributes.Color := clRed;
                 SelStart := Tag;
               end;
      end;
      Key := #0;
      Caption := PswStr; //在Form.Caption显示输入的字符
    end;procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      Key := 0;
    end;