Jump to content
Sign in to follow this  
IElite

Select controls text ?

Recommended Posts

Doesn't look like there is a delphi eqivalent of "SelLength, SelStart, SelText" for the EditBox or Memo controls.

How do I set focus to a specific control on form when form is activated?

How do I select all the text in a control when the control has focus?

 

i have tried all of the following

fMemo.SetContentSelectionMode(tsmAll);
fMemo.Handle.Focused:= True;
fMemo.Handle.SelectAll;
 handle.getElementById('memo').focus();
 handle.getElementById('memo').select();

thanx

Share this post


Link to post
Share on other sites

you'll probably have to do it after the memo or editbox has been constructed in the dom

A form with 2 editboxes and the following code works for me.

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  Handle.ReadyExecute( procedure ()
  begin
    W3EditBox2.Text := 'dadadada';
    W3EditBox2.handle.select();
    W3EditBox2.handle.focus();
    W3EditBox2.handle.setSelectionRange(2,5);
  end);
end;

If you need this on a regular basis you can extend the editbox component

Cheers

Share this post


Link to post
Share on other sites

Your code works good for when the form is first displayed, but when I tab through controls or click on control, it doesn't seem to work

e.g.

procedure TfrmComplete.HandleGotFocus(Sender: TObject);
begin
 Handle.ReadyExecute( procedure ()
 begin
    fMemo.handle.select();
    fMemo.handle.focus();
 end);
end;

procedure TfrmComplete.InitializeObject;
begin
  inherited;
  {$I 'Complete:impl'}
  fMemo:= TW3Memo.Create(self);
  fMemo.Text:= '<None>';
  fMemo.OnGotFocus:= HandleGotFocus;
end;

 

Share this post


Link to post
Share on other sites

You may not particularly like this solution, however it does work

Solution is to go deeper into the DOM : send out an internal message triggered by regular focus and click events and have a dedicated listener for those events.

Have a Form with 3 regular Memo's :

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components

  //initially select Memo2 on form init
  Handle.ReadyExecute( procedure ()
  begin
    browserapi.window.postMessage('W3Memo2','*');
  end);

  // event listener for this message type
  browserapi.window.onmessage := procedure(msg: variant)
  begin
    writeln('received ' + msg.data);
    If msg.data = 'W3Memo1' then begin
      W3Memo1.handle.select();
      W3Memo1.handle.focus();
      W3Memo1.handle.setSelectionRange(2,5);
    end;
    If msg.data = 'W3Memo2' then begin
      W3Memo2.handle.select();
      W3Memo2.handle.focus();
      W3Memo2.handle.setSelectionRange(2,5);
    end;
    If msg.data = 'W3Memo3' then begin
      W3Memo3.handle.select();
      W3Memo3.handle.focus();
      W3Memo3.handle.setSelectionRange(2,5);
    end;
  end;

  //send message on regular focus and click events
  W3Memo1.handle.onfocus := lambda browserapi.window.postMessage('W3Memo1','*'); end;
  W3Memo2.handle.onfocus := lambda browserapi.window.postMessage('W3Memo2','*'); end;
  W3Memo3.handle.onfocus := lambda browserapi.window.postMessage('W3Memo3','*'); end;
  W3Memo1.handle.onclick := lambda browserapi.window.postMessage('W3Memo1','*'); end;
  W3Memo2.handle.onclick := lambda browserapi.window.postMessage('W3Memo2','*'); end;
  W3Memo3.handle.onclick := lambda browserapi.window.postMessage('W3Memo3','*'); end;

end;

This can be streamlined a bit by stringifying the element name or id in the postMessage data and using that in its event listener

If you want to use the regular RTL then you'll find there is quite a bit of processing going on around 'focus' events. Not sure how exactly to use in your case except for the above solution

Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×