执行超出默认行为的任何操作都可能非常危险TEdit
控制。您的用户知道标准 Windows 控件的行为方式,任何偏离此的行为都可能导致混乱。
默认情况下AutoSelect http://docwiki.embarcadero.com/Libraries/en/Vcl.StdCtrls.TCustomEdit.AutoSelect属性设置为True
.
确定当控件获得焦点时是否自动选择编辑控件中的所有文本。
Set 自动选择当编辑控件获得焦点时选择所有文本。自动选择仅适用于单行编辑控件。
Use 自动选择当用户更有可能替换编辑控件中的文本而不是追加到其中时。
When this property is True
, the entire contents of the edit control are selected when it gets the focus by means of keyboard action. If the control gets the focus by a mouse click then the contents will not all be selected. In that case you simply press CTRL+A to select all. A double click will select the word underneath the mouse. This is all standard behaviour implemented by the underlying Windows control.
如果您更改选择以响应OnClick
事件,根据当前选择的答案,那么您会发现无法通过单击鼠标来移动插入符号。这是极其违反直觉的行为。
这是一个典型的例子,说明了为什么在更改控件的默认行为时需要非常小心。在测试时很容易不错过特定的用例,但是当您的用户掌握该程序时,他们肯定会发现所有这些问题。
您可以安全地做的就是致电SelectAll
from OnDblClick
。我相信这不会有令人讨厌的副作用。
另一种选择是致电SelectAll
当焦点切换到编辑控件时,但不是每次单击控件时。这对用户来说可能感觉有点奇怪,但我个人认为采取这种做法是合理的。如果你想这样做,你需要处理OnEnter
编辑控件的事件:
procedure TForm1.Edit1Enter(Sender: TObject);
begin
PostMessage(Edit1.Handle, EM_SETSEL, 0, -1);
end;