简体   繁体   English

如何在TEdit onkeyup事件中将字符更改为星号?

[英]How change characters to asterisk on TEdit onkeyup event?

my client requested to me for make a "effect" where each time that release any key of keyboard, change this character to asterisk on field. 我的客户要求我做一个“效果”,每次释放键盘的任何键,在字段上将此字符更改为星号。

How do this in Delphi? 德尔福怎么样?

I have in Html + Javascript like this . 我有像这样的 Html + Javascript。 .js code can be found here . .js代码可以在这里找到。

There is no way to achieve what you want with the standard Delphi controls. 使用标准的Delphi控件无法实现您的目标。

The VCL edit control uses the feature of the underlying Windows EDIT control to mask input characters for "Password" type edit controls. VCL编辑控件使用基础Windows EDIT控件的功能来屏蔽“密码”类型编辑控件的输入字符。 The behaviour is therefore determined by the Windows (OS) control, not Delphi itself. 因此,行为由Windows(OS)控件决定,而不是由Delphi本身决定。

You could try to get the effect you need by using a non-password field and handling key events to replace characters as required with asterisks or any other masking character, but you would also need to separately maintain the intended content of the edit control. 您可以尝试通过使用非密码字段和处理键事件来根据需要用星号或任何其他屏蔽字符替换字符来获得所需的效果,但您还需要单独维护编辑控件的预期内容。

This would almost certainly be easier to implement as a custom edit control, rather than trying to customize the behaviour of a standard edit control with events. 这几乎可以更容易地实现为自定义编辑控件,而不是尝试使用事件自定义标准编辑控件的行为。

I suspect that the implementation of a custom control is not the solution you are after however. 我怀疑自定义控件的实现并不是你所追求的解决方案。

The below simulates the behavior of the js code with the difference backspace is also handled. 下面模拟了js代码的行为,并且还处理了差异退格。

type
  TForm2 = class(TForm)
    Edit1: TEdit;
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  private
    FEditText: string;

procedure TForm2.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = ^H then
    SetLength(FEditText, Length(FEditText) - 1)
  else
    FEditText := FEditText + Key;
end;

procedure TForm2.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  Edit1.Text := StringOfChar('*', Length(Edit1.Text));
  Edit1.SelStart := Length(Edit1.Text);
end;

FEditText is the equivalent of df[0].Value in js code, the actual value that is typed. FEditText相当于js代码中的df[0].Value ,即输入的实际值。

Note that there's no option to reset the text, as there is none in the js code. 请注意,没有重置文本的选项,因为js代码中没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM