Hi,
I am trying to have a RVE select all text when the user clicks in it (i use one as a one-line editor).
If i write RVE.SelectAll at the OnEnter event, RVE will flash momentarily with the selection, and then lose it.
I tried putting a 'PostMessage' there but it does not work either.
Any help pls?
TIA
Costas
How to select all text when user clicks a RVE?
-
- Site Admin
- Posts: 17566
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
-
- Site Admin
- Posts: 17566
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
If I exclude rvoWantTabs from rve.EditorOptions, and add rve.SelectAll in rve.OnEnter, then if you change the input focus using tabs, it is selected on focusing. However, when the focus is moved with the mouse, it is deselected when the mouse button is released.
Talking about the standard behavior, I noticed that it is different for TEdit and TComboBox.
TEdit is selected only if the focus is moved with the keyboard.
TComboBox (including IE's address bar) is selected both with mouse and the keyboard.
To simulate TEdit's selection, you can write:
This code selects only if the focus is obtained when the left mouse button is not pressed (well, the user can hold this button and press Tab, but it is a rare exception).
Unfortunately, simulating TComboBox is more difficult, I cannot give a solution right now...
Talking about the standard behavior, I noticed that it is different for TEdit and TComboBox.
TEdit is selected only if the focus is moved with the keyboard.
TComboBox (including IE's address bar) is selected both with mouse and the keyboard.
To simulate TEdit's selection, you can write:
Code: Select all
procedure TForm1.RichViewEdit1Enter(Sender: TObject);
var KeyboardState: TKeyboardState;
begin
GetKeyboardState(KeyboardState);
if KeyboardState[VK_LBUTTON] and $80 = 0 then
RichViewEdit1.SelectAll;
end;
Unfortunately, simulating TComboBox is more difficult, I cannot give a solution right now...
-
- Site Admin
- Posts: 17566
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
May be this is a not elegant, but it looks like it works:
FocusedWithMouse is a form's Boolean field, it equals to False initially.
Code: Select all
function TForm1.IsPressed(rve: TControl): Boolean;
var KeyboardState: TKeyboardState;
pt: TPoint;
begin
GetKeyboardState(KeyboardState);
Result := (KeyboardState[VK_LBUTTON] and $80 <> 0);
if Result then begin
GetCursorPos(pt);
pt := rve.ScreenToClient(pt);
Result := (pt.x>=0) and (pt.y>=0) and (pt.x<=rve.ClientWidth) and
(pt.y<=rve.ClientHeight);
end;
end;
procedure TForm1.RichViewEdit1Enter(Sender: TObject);
begin
FocusedWithMouse := IsPressed(Sender as TControl);
(Sender as TRichViewEdit).SelectAll;
end;
procedure TForm1.RichViewEdit1RVMouseUp(Sender: TCustomRichView;
Button: TMouseButton; Shift: TShiftState; ItemNo, X, Y: Integer);
begin
if FocusedWithMouse then begin
Sender.SelectAll;
Sender.Invalidate;
FocusedWithMouse :=False;
end;
end;