Page 1 of 1

How to select all text when user clicks a RVE?

Posted: Wed Sep 01, 2010 9:58 am
by csterg
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

Posted: Wed Sep 01, 2010 5:48 pm
by Sergey Tkachenko
Do you want to simulate TEdit?
But TEdit is selected only if it is focused using the keyboard, not mouse...

Posted: Wed Sep 01, 2010 6:02 pm
by csterg
Yes, but if you write a 'SelectAll' on the Enter message, it stays selected. RVE does not. This behavior is kind of standard (e.g. address bar of browser, etc)
Costas

Posted: Wed Sep 01, 2010 6:25 pm
by Sergey Tkachenko
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:

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;
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...

Posted: Wed Sep 01, 2010 6:28 pm
by csterg
I was looking exactly at what is hard to implement...
I even tried with a timer event with a delay of 200ms, but still i can't get the effect; i thought this would work...
Costas

Posted: Wed Sep 01, 2010 7:08 pm
by Sergey Tkachenko
May be this is a not elegant, but it looks like it works:

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;
FocusedWithMouse is a form's Boolean field, it equals to False initially.

Posted: Sat Sep 18, 2010 1:02 pm
by csterg
Works great, thank you