Author Topic: Strangely - Not a question  (Read 494 times)

J-G

  • Hero Member
  • *****
  • Posts: 1287
Strangely - Not a question
« on: August 12, 2026, 12:15:35 pm »
Whenever I post here I'm usually seeking answers to issues that elude me but Today I have had a small victory over that ways that Lazarus/FPC endeavours to thwart me without the need to interupt the very busy lives that contributors here have  ;D

The project involves entering a HEX number into a TEdit and since there is no Property 'HexOnly' (as there is a NumbersOnly (with limited usefulness)) and I wish to trap 'Invalid Key Strokes' to display meaningful information, I set about writing a 'Key Validation' routine. Nothing really taxing once you get your head around the difference between 'KeyNumber' and 'ScanCode' but I had one 'silly' irritation.

When an invalid character was used, I could trap it, determine what it was, show the user what key was unacceptable and remove it from the 'string', All well and good BUT the editing position within the string was always set at the start - rather than the position that the invalid key was typed.

At this point I didn't even know what 'property' I needed to modify to set the 'Cursor' position. A little research and I discovered 'CaretPos' and sure enough I soon found that as a 'Property' of the TEdit. Now I could store this property at the start of the validation and retrieve it at the end  - - -  'simples' - - -  wrong!

Yes, I could assign it to a var at the start - though initially I declared that as 'Byte' when in fact it's a 'TPoint' but that was a minor issue - and restore it at the end,  but the result was that the position was always one character to the right of the original ???

Ah - I thought - take 1 off the TEdit.CaretPos 'X'  - - -   again wrong, compiler says 'not allowed'  -  Next attempt, modify the Var (which of course is also a TPoint but not a 'property' of the TEdit).  That is allowed, so, assign the Var, take one off the 'X' property then at the end of the proc. assign the TEdit.CaretPos to the Var  - - -  fully acceptable.

I said at the start that I wasn't interupting your busy lives but if you've read this far of course I have  :( sorry - I hope that it is minimal  -  but this tale may assist someone else who needs to know where the cursor actually is (for whatever reason!)  - and it's always good to share a 'success' 🤣



FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

cdbc

  • Hero Member
  • *****
  • Posts: 2944
    • http://www.cdbc.dk
Re: Strangely - Not a question
« Reply #1 on: August 12, 2026, 12:26:44 pm »
Hi J-G
Ahh, back from holiday... \o/
?) You do know that 'CaretPos' deals in 'CodePoint's -- -NOT- in chars/bytes ?!?
Might bite you on the ass, with regards to unicode-text / UTF8-text  ;D :D 8)
Just saying...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

olatov

  • Full Member
  • ***
  • Posts: 111
Re: Strangely - Not a question
« Reply #2 on: August 12, 2026, 01:19:02 pm »
I remember using OnKeyPress event for stuff like this - you simply filter keys, resetting invalid ones to #0, that should not interfere with the position of the cursor. It will not save you from pasting invalid chars using a mouse, though.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   case Key of
  4.     '0'..'9', 'a'..'f', 'A'..'F', #8:   { #8: Backspace }
  5.       ErrorLabel.Caption := 'OK';
  6.   else
  7.     ErrorLabel.Caption := 'Invalid character - "' + Key + '"';
  8.     Key := #0;
  9.   end;
  10. end;  

Dankan1890

  • New Member
  • *
  • Posts: 18
Re: Strangely - Not a question
« Reply #3 on: August 12, 2026, 01:56:56 pm »
TMaskEdit ?

J-G

  • Hero Member
  • *****
  • Posts: 1287
Re: Strangely - Not a question
« Reply #4 on: August 12, 2026, 02:15:12 pm »
TMaskEdit ?
I've tried that in the past and found it a PITA  -  far too restrictive.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1287
Re: Strangely - Not a question
« Reply #5 on: August 12, 2026, 03:20:38 pm »
Hi J-G
Ahh, back from holiday... \o/
?) You do know that 'CaretPos' deals in 'CodePoint's -- -NOT- in chars/bytes ?!?
Might bite you on the ass, with regards to unicode-text / UTF8-text  ;D :D 8)
Just saying...
Regards Benny
Hi Benny - hope you had a pleasant one  :D

An interesting point and a very valid observation. In my use case though I'm only dealing with simple Keyboard entry - though I could forsee a user trying the [Alt][Keypad] aproach - but I'm really looking to trap "typo's" where (say) a 'Y' key gets pressed rather than a '6' or '7' - but I have handled all potential single key-strokes  -  I may look at trapping the [Alt] Key as well,  though I suspect  . . . . . .

[Later]   
I've now trapped the potential [Alt] KeyPad entry  :o   Better to cover all bases !
I was thinking that the [Alt] key might be trapped by some 'Windows' system call before a programmer could gain access. - in fact that may be the case but once a NumPad key has been pressed then it can be tested using the if Shift = [ssAlt] then construct.

There can never be a UNICODE entry in this particular use - essentially I suppose what I'm doing is creating what was 'ReadKey' under a DOS environment. It's just taken me some 15+ years to get around to it 🤣

The project doesn't expect the user to 'Enter' a UNICODE ref. it takes Decimal. Hex or Binary input and evaluates a Unicode value.



FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

ASerge

  • Hero Member
  • *****
  • Posts: 2518
Re: Strangely - Not a question
« Reply #6 on: August 12, 2026, 03:31:27 pm »
A little research and I discovered 'CaretPos' and sure enough I soon found that as a 'Property' of the TEdit.
For TEdit (not TMemo), it’s better to use SelStart. It’s simple:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Edit1.AutoSelect := False;
  4.   Edit1.Text := 'wrOong char';
  5.   Edit1.SelStart := 2;
  6.   Edit1.SelLength := 1;
  7.   ActiveControl := Edit1;
  8. end;
  9.  
  10. procedure TForm1.Button2Click(Sender: TObject);
  11. begin
  12.   Edit1.SelText := '';
  13.   ActiveControl := Edit1;
  14. end;

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 769
Re: Strangely - Not a question
« Reply #7 on: August 12, 2026, 05:21:33 pm »
Ah that wonderful endorphin rush when you finally crack and solve a problem after a day or two of frustration! :)

J-G

  • Hero Member
  • *****
  • Posts: 1287
Re: Strangely - Not a question
« Reply #8 on: August 12, 2026, 05:27:42 pm »
Ah that wonderful endorphin rush when you finally crack and solve a problem after a day or two of frustration! :)

It's 'Palpable' 😂  -  mine are usually longer than 'a day or two' !
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Bart

  • Hero Member
  • *****
  • Posts: 5776
    • Bart en Mariska's Webstek
Re: Strangely - Not a question
« Reply #9 on: August 12, 2026, 10:26:58 pm »
TMaskEdit ?
I've tried that in the past and found it a PITA  -  far too restrictive.

Yes, but you can set an EditMask so that it only ever accepts Hex characters (upper- or lowercase as you wish), and you cannot even paste non-Hex into it.
You may find that too restrictive, but it seems it does exactly what you say you want?

Bart

 

TinyPortal © 2005-2018