Delphi 进入TEdit控件时选中该控件中的内容,不使用SelectAll
2010-08-25 17:56
387 查看
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.
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.
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 ;)
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 istabbed 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控件和TListBox控件组合使用达到TComboBox控件一样的效果呢?
- 在对话框中使用CTreeCtrl控件时,在OnNMClick消息中如何获取选中的Item以及内容
- Delphi里使用SPComm串口控件的一个乌龙BUG
- 使用 ExpanderView 控件动态递归呈现内容
- Delphi连接Oracle控件ODAC的安装及使用
- Delphi 在DLL中使用DevExpress控件时出错解决办法
- DELPHi第三方控件使用方法(摘录)
- Delphi 7下使用VT实现树型列表结合控件
- 验证控件内容是否为空写法二(使用服务器控件进行非空验证的前台代码)
- 解决使用ajaxFileUpload上传控件出现的问题:回调函数总是进入error或success
- delphi中XLSReadWrite控件的使用(2)---delphi XE下安装
- 让CEdit控件SetSel后选中其中内容
- delphi中XLSReadWrite控件的使用(1)---简介
- 如何在Delphi中使用Microdraw图形控件的OLEVariant类型数据
- delphi中使用透明控件的几种方法
- 转Delphi中XLSReadWrite控件的使用(1)---简介
- 在Delphi中使用IP控件
- 使用js获得html控件的style属性内容
- C#Winform基础 显示选中行的第一列单元格的内容,datagridview控件
- Delphi 重绘TEdit显示的内容