How to select all text when user clicks a RVE?

General TRichView support forum. Please post your questions here
Post Reply
csterg
Posts: 306
Joined: Fri Nov 25, 2005 9:09 pm

How to select all text when user clicks a RVE?

Post 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
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do you want to simulate TEdit?
But TEdit is selected only if it is focused using the keyboard, not mouse...
csterg
Posts: 306
Joined: Fri Nov 25, 2005 9:09 pm

Post 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
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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...
csterg
Posts: 306
Joined: Fri Nov 25, 2005 9:09 pm

Post 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
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post 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.
csterg
Posts: 306
Joined: Fri Nov 25, 2005 9:09 pm

Post by csterg »

Works great, thank you
Post Reply