TEdit onclick 全选?
每当用户单击 TEdit1 或单击选择其中的某些文本时,如何选择 TEdit1 的所有文本
How to select all text of a TEdit1 whenever user click on it or click to select some text of it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
执行超出
TEdit
控件默认行为的任何操作都可能非常危险。您的用户知道标准 Windows 控件的行为方式,任何偏离此的行为都可能导致混乱。默认情况下,
AutoSelect
属性为设置为True
。当此属性为
True
时,当编辑控件通过键盘操作获得焦点时,将选择编辑控件的全部内容。如果控件通过鼠标单击获得焦点,则内容将不会全部被选择。在这种情况下,您只需按 CTRL+A 即可选择全部。双击将选择鼠标下方的单词。这是底层 Windows 控件实现的所有标准行为。如果您根据当前选定的答案更改响应
OnClick
事件的选择,那么您会发现无法通过单击鼠标来移动插入符号。这是极其违反直觉的行为。这是一个典型的例子,说明了为什么在更改控件的默认行为时需要非常小心。在测试时很容易不错过特定的用例,但是当您的用户掌握该程序时,他们肯定会发现所有这些问题。
您可以安全地做的是从
OnDblClick
调用SelectAll
。我相信这不会产生令人讨厌的副作用。另一种选择是在焦点切换到编辑控件时调用
SelectAll
,但不是每次单击控件时都调用。这对用户来说可能感觉有点奇怪,但我个人认为采取这种做法是合理的。如果您想执行此操作,您需要处理编辑控件的OnEnter
事件: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 toTrue
.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
fromOnDblClick
. 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 theOnEnter
event of your edit control:在 VCL 编辑器中选择 Edit1,然后双击
OnClick
事件:您还可以将此事件链接到另一个控件,例如按钮。
选择该按钮,选择并单击
V
箭头以选择要链接的事件。现在
Edit1.OnClick
和Button1.OnClick
链接到同一事件。Select Edit1 in the VCL editor and double-click on the
OnClick
event: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.Now both
Edit1.OnClick
andButton1.OnClick
link to the same event.如何在用户单击 TEdit1 时选择它的某些文本:
How to select some text of a TEdit1 whenever user click on it:
您必须使用OnMouseUp;
You must use OnMouseUp;