TEdit onclick 全选?

发布于 2024-12-22 15:50:13 字数 52 浏览 3 评论 0原文

每当用户单击 TEdit1 或单击选择其中的某些文本时,如何选择 TEdit1 的所有文本

How to select all text of a TEdit1 whenever user click on it or click to select some text of it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-12-29 15:50:13

执行超出 TEdit 控件默认行为的任何操作都可能非常危险。您的用户知道标准 Windows 控件的行为方式,任何偏离此的行为都可能导致混乱。

默认情况下, AutoSelect 属性为设置为True

确定当控件获得焦点时是否自动选择编辑控件中的所有文本。

设置自动选择以在编辑控件获得焦点时选择所有文本。 自动选择仅适用于单行编辑控件。

当用户更有可能替换编辑控件中的文本而不是追加到其中时,请使用自动选择

当此属性为True时,当编辑控件通过键盘操作获得焦点时,将选择编辑控件的全部内容。如果控件通过鼠标单击获得焦点,则内容将不会全部被选择。在这种情况下,您只需按 CTRL+A 即可选择全部。双击将选择鼠标下方的单词。这是底层 Windows 控件实现的所有标准行为。


如果您根据当前选定的答案更改响应 OnClick 事件的选择,那么您会发现无法通过单击鼠标来移动插入符号。这是极其违反直觉的行为。

这是一个典型的例子,说明了为什么在更改控件的默认行为时需要非常小心。在测试时很容易不错过特定的用例,但是当您的用户掌握该程序时,他们肯定会发现所有这些问题。

您可以安全地做的是从 OnDblClick 调用 SelectAll。我相信这不会产生令人讨厌的副作用。

另一种选择是在焦点切换到编辑控件时调用 SelectAll,但不是每次单击控件时都调用。这对用户来说可能感觉有点奇怪,但我个人认为采取这种做法是合理的。如果您想执行此操作,您需要处理编辑控件的 OnEnter 事件:

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  PostMessage(Edit1.Handle, EM_SETSEL, 0, -1);
end;

It can be quite dangerous to do anything beyond the default behaviour of the TEdit control. Your users know how the standard Windows controls behave and any deviation from this is likely to cause confusion.

By default the AutoSelect property is set to True.

Determines whether all the text in the edit control is automatically selected when the control gets focus.

Set AutoSelect to select all the text when the edit control gets focus. AutoSelect only applies to single-line edit controls.

Use AutoSelect when the user is more likely to replace the text in the edit control than to append to it.

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.


If you change the select in response to the OnClick event, as per the currently selected answer, then you will find that it is impossible to move the caret with a mouse click. This is exceedingly counter-intuitive behaviour.

This is a classic example of why you need to be very careful about changing the behaviour of a control from its default. It's simply very easy not to miss a particular use case when testing but when your users get hold of the program, they are sure to find all such wrinkles.

What you could safely do is to call SelectAll from OnDblClick. This would, I believe have no annoying side-effects.

Another option would be to call SelectAll when the focus switched to the edit control, but not every time you click in the control. This might feel a little odd to the user, but I personally think it would be reasonable to take this course of action. If you want to do this you need to handle the OnEnter event of your edit control:

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  PostMessage(Edit1.Handle, EM_SETSEL, 0, -1);
end;
清旖 2024-12-29 15:50:13

当用户点击 TEdit1 时如何选择它的所有文本

在 VCL 编辑器中选择 Edit1,然后双击 OnClick 事件:

在此处输入图像描述

procedure TForm13.Edit1Click(Sender: TObject);
begin
  Edit1.SelectAll;
end;

您还可以将此事件链接到另一个控件,例如按钮。
选择该按钮,选择并单击 V 箭头以选择要链接的事件。

在此处输入图像描述

现在 Edit1.OnClickButton1.OnClick 链接到同一事件。

How to select all text of a TEdit1 whenever user click on it

Select Edit1 in the VCL editor and double-click on the OnClick event:

enter image description here

procedure TForm13.Edit1Click(Sender: TObject);
begin
  Edit1.SelectAll;
end;

You can also link this event to another control like a button.
Select the button, choose and click on the V arrow to select an event you want to link.

enter image description here

Now both Edit1.OnClick and Button1.OnClick link to the same event.

故人如初 2024-12-29 15:50:13

如何在用户单击 TEdit1 时选择它的某些文本:

procedure TForm1.Edit1Click(Sender: TObject);
begin
  Edit1.SelStart:= 1;
  Edit1.SelLength:= 2;
end;

How to select some text of a TEdit1 whenever user click on it:

procedure TForm1.Edit1Click(Sender: TObject);
begin
  Edit1.SelStart:= 1;
  Edit1.SelLength:= 2;
end;

您必须使用OnMouseUp;

procedure cxMRUEdit1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if Button=mbLeft then cxMRUEdit1.SelectAll;
end;

You must use OnMouseUp;

procedure cxMRUEdit1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if Button=mbLeft then cxMRUEdit1.SelectAll;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文