Delphi 进入TEdit控件时选中该控件中的内容,不使用SelectAll
来源:互联网 发布:奥迪tt知乎 编辑:程序博客网 时间:2024/04/28 12:08
T(Custom)Edit's AutoSelect property determines whether all the text in the edit control is automatically selected when the control gets focus.
The AutoSelect property has the value of TRUE by default - when the control receives the focus, i.e. when the control is tabbed into all the text in the control will be selected.
And here's a problem! The AutoSelect will auto select the text in an edit control only when the control receives the focus as a result of TAB key.
AutoSelect On Click Focus
What I need is that all the text is selected even when the edit control receives the focus from a mouse click.
There's the OnEnter event which gets fired when the control receives the focus. There's a SelectAll method which selects all text in the edit control. When you click into a non-focused edit control, it will receive the focus and the OnEnter event will get fired.
If you put "Edit.SelectAll" in the OnEnter event, presuming that the text will be selected when the control receives the focus - you are wrong :(
The default behavior for the OnClick event sets the caret to the clicked position, clearing any selection if there was one.
The OnClick is called after the OnEnter, this is why calling SelectAll in OnEnter will not work - the "hidden" OnClick behavior will clear the selection.
Also, you cannot simply place the call to SelectAll in the OnClick event handler as every time the user clicks into the edit control that already has the input focus - all the text will be selected.
SelectAll On First OnClick Focus
What we want is that AutoSelect works not only when the control is tabbed into BUT also when the edit control receives the input focus as a result of a mouse click into the control.
.
And finally, here's a simple trick to make this happen:
//Edit1 OnClick
procedure TEntryForm.Edit1Click(Sender: TObject);
begin
Edit1.SelectAll;
Edit1.OnClick := nil;
end;
//Edit1 OnExit (lost focus)
procedure TEntryForm.Edit1Exit(Sender: TObject);
begin
Edit1.OnClick := Edit1Click;
end;
When the OnClick event is fired, the event handler select all the text, then detaches the event handling procedure (Edit1Click) from the event handler.
This ensures the auto selection will not happen as a result of mouse clicks inside the edit WHILE the edit has the input focus.
To restore the OnClick event handler, set it back again in the OnExit event - when the input focus shifts away from the edit control to another.
Of course, you basically loose the OnClick functionality while the edit has the focus ... but you can live without that ;)
- Delphi 进入TEdit控件时选中该控件中的内容,不使用SelectAll
- Delphi中如何使用TEdit控件和TListBox控件组合使用达到TComboBox控件一样的效果呢?
- CEdit控件内容选中
- DropDownList控件应用之TextBox或另一个DropDownList控件动态显示该选中项指定内容
- Delphi 重绘TEdit显示的内容
- delphi中的realAudio ActiveX控件的使用
- Delphi控件TEdit只接受数字键和退格键的实现方法
- Delphi中如何让TEdit编辑框控件从右边开始输入数据呢?
- Delphi中的DBGrid控件
- Delphi中的DBGrid控件
- 使用repeater控件时,得到选中checkbox
- delphi 按回车选中下一个控件
- delphi 使用进度条控件
- delphi 图表控件使用
- delphi控件使用
- delphi---控件使用
- delphi控件使用
- 选中Edit控件中的文本
- 关于getObjectsUnderPoint
- [SQL Server] 在实例间传输用户名和密码信息
- 简单的例子把OO的JavaScript说明白
- 只有在配置文件或 Page 指令中将 enableSessionState 设置为 true 时,才能使用会话状态。还请确保在应用程序配置的 // 节中包括 System.Web.SessionStateMod 或自定义会话状态模块。
- Acrobat vc 调用 js
- Delphi 进入TEdit控件时选中该控件中的内容,不使用SelectAll
- poj 2828 线段树,思路跟 3667hotel差不多. 感觉比那个简单一点 由此说明. hotel 是十分经典到的有区间里面讨论的线段树
- 人为什么会痛苦?
- windows核心编程第六章--线程
- Android线程学习
- linux环境sqlplus中使用backspace 及 方向键
- iPhone中获取磁盘空间方法
- sizeof
- 基于EfsFrame框架的主从表操作(查询、修改、删除)