Recent

Author Topic: [Solved] How to unselect the text in a focused TEdit, programmatically  (Read 1894 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1774
    • http://www.cdbc.dk
Hi
I'm having a 'Brain-fart'  %)
I can't seem to figure out how to un-select the text in a focused TEdit, programmatically from within the containing form...?!?
Any help will be greatly appreciated.
Regards Benny
« Last Edit: November 25, 2024, 08:25:40 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ackarwow

  • Full Member
  • ***
  • Posts: 127
    • Andrzej Karwowski's Homepage
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #1 on: November 25, 2024, 06:07:59 pm »
Maybe Edit.SelLength:=0 will be sufficient?

dsiders

  • Hero Member
  • *****
  • Posts: 1324
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #2 on: November 25, 2024, 06:12:12 pm »
Hi
I'm having a 'Brain-fart'  %)
I can't seem to figure out how to un-select the text in a focused TEdit, programmatically from within the containing form...?!?
Any help will be greatly appreciated.
Regards Benny

TEdit.ClearSelection
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

cdbc

  • Hero Member
  • *****
  • Posts: 1774
    • http://www.cdbc.dk
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #3 on: November 25, 2024, 06:35:27 pm »
Hi
Sorry guys, neither of your suggestions worked...
I'd like for the user to type onto the end of the pretext, but alas, when it's selected, the pretext just gets overwritten.
I've also tried:
Code: Pascal  [Select][+][-]
  1.   edtMask.SelStart:= bcLengthUTF8(edtMask.Text);
  2.   edtMask.SelLength:= 0;
  3. or
  4.   edtMask.Perform(LM_KEYDOWN,VK_END,VK_END);
  5.   edtMask.Perform(LM_KEYUP,VK_END,VK_END);
  6.  
Hmmm, what's next, to try...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #4 on: November 25, 2024, 06:40:28 pm »
TEdit.ClearSelection

TEdit.ClearSelection: Removes the selected text from the edit control.

I think that's not what TS wants.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #5 on: November 25, 2024, 06:42:32 pm »
Are you using a TMaskEdit?
In that case: it'll always select at leas one character in the control when you type or tab into the control.

Setting SelLength to zero should just work (also for a TMaskEdit).

Bart

cdbc

  • Hero Member
  • *****
  • Posts: 1774
    • http://www.cdbc.dk
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #6 on: November 25, 2024, 06:43:05 pm »
Hi
Thanks Bart - No it's not and no it didn't... %)
Plain ol' TEdit.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

ackarwow

  • Full Member
  • ***
  • Posts: 127
    • Andrzej Karwowski's Homepage
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #7 on: November 25, 2024, 06:43:30 pm »
Hi
Sorry guys, neither of your suggestions worked...
I'd like for the user to type onto the end of the pretext, but alas, when it's selected, the pretext just gets overwritten.
I've also tried:
Code: Pascal  [Select][+][-]
  1.   edtMask.SelStart:= bcLengthUTF8(edtMask.Text);
  2.   edtMask.SelLength:= 0;
  3. or
  4.   edtMask.Perform(LM_KEYDOWN,VK_END,VK_END);
  5.   edtMask.Perform(LM_KEYUP,VK_END,VK_END);
  6.  
Hmmm, what's next, to try...
Regards Benny

@Benny
When do you call this code (what event)?

cdbc

  • Hero Member
  • *****
  • Posts: 1774
    • http://www.cdbc.dk
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #8 on: November 25, 2024, 06:46:26 pm »
Hi
Quote
When do you call this code (what event)?
It gets called just after I assign the 'pretext' to edtMask.Text, in the 'change-handler' for edtMask...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

dsiders

  • Hero Member
  • *****
  • Posts: 1324
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #9 on: November 25, 2024, 06:55:58 pm »
TEdit.ClearSelection

TEdit.ClearSelection: Removes the selected text from the edit control.

I think that's not what TS wants.

Bart

Doh. My bad.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

ackarwow

  • Full Member
  • ***
  • Posts: 127
    • Andrzej Karwowski's Homepage
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #10 on: November 25, 2024, 06:58:42 pm »
Hi
Quote
When do you call this code (what event)?
It gets called just after I assign the 'pretext' to edtMask.Text, in the 'change-handler' for edtMask...
Regards Benny

You can try to change Edit1.AutoSelect property (if checked) into 'false' and then in change handler add something like this: Edit1.SelStart:=length(Edit1.Text);
(Edit1 is your TEdit)

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #11 on: November 25, 2024, 07:02:00 pm »
This code moves the caret to the end of the text and unselects everything in a TEdit
Code: Pascal  [Select][+][-]
  1.   Edit1.SelStart := MaxInt;
  2.   Edit1.SelLength := 0;

Works fine for me.

Bart

cdbc

  • Hero Member
  • *****
  • Posts: 1774
    • http://www.cdbc.dk
Re: how to unselect the text in a focused TEdit, programmatically
« Reply #12 on: November 25, 2024, 07:45:53 pm »
Hi
@Bart, hmm - not so much here... :/
Through fiddling, I got it to work... I think  %)
I ended up with this solution:
Code: Pascal  [Select][+][-]
  1. procedure TfrmWords.DelayedShow(aData: ptrint);  // edtMask.SelStart:=  bcLengthUTF8(edtMask.Text)
  2. begin
  3.   edtMask.SelStart:= aData;
  4.   edtMask.SelLength:= 0;
  5. end;
  6.  
  7. procedure TfrmWords.FormShow(Sender: TObject);
  8. begin
  9.   Application.QueueAsyncCall(@DelayedShow,bcLengthUTF8(edtMask.Text));
  10. end;
  11.  
  12. procedure TfrmWords.edtMaskChange(Sender: TObject);
  13. begin { 'Text' property is the safer bet, 'cause the wordcompletion could come from a lib }
  14.   if fStartsWith then lbxWords.Items.Text:= fwc.GetCompletedPrefix(edtMask.Text,false).Text { no is/as }
  15.   else fwc.GetWordList(lbxWords.Items,edtMask.Text,true,false,256);
  16.   if lbxWords.Items.Count > 0 then lbxWords.ItemIndex:= 0;
  17. end;
Thank you for all of your suggestions...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: [Solved] How to unselect the text in a focused TEdit, programmatically
« Reply #13 on: December 01, 2024, 12:14:50 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Enter(Sender: TObject);
  2. begin
  3.   Exit;
  4.   With TEdit(Sender) do
  5.    Begin
  6.      SelStart := CaretPos.X;
  7.      SelLength := 0;
  8.    end;
  9. end;                            
  10.  

How's that?
 :-[
The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: [Solved] How to unselect the text in a focused TEdit, programmatically
« Reply #14 on: December 01, 2024, 07:07:37 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Enter(Sender: TObject);
  2. begin
  3.   Exit;
  4.   With TEdit(Sender) do
  5.    Begin
  6.      SelStart := CaretPos.X;
  7.      SelLength := 0;
  8.    end;
  9. end;                            
  10.  
@jamie: anything after exit will not be executed
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018