1

Good afternoon,

I'm doing a project in delphi that uses editmask. I'm using the phone mask. When clicking on edit to write the phone number, it goes to the last field on the right, so it is necessary to go back with the backspace to the beginning of the edit on the left.

i would like to find a way that when the user typed the number in the last field on the right, it was passed to the left. So on until you complete the phone field. It would be possible?

Using an example of what it would look like:

enter image description here

enter image description here

enter image description here

I couldn't think of a way to do it

1 Answers1

2

The component is called TMaskEdit.

Just like anything that bases on TEdit putting the focus onto the control will by default put the text cursor at the end of its content

  • via keyboard, should .AutoSelect be FALSE and
  • via mouse if clicking behind any text (by default the text is aligned to the left).

You should have experienced this with the other components already. If you want the text cursor to always be at a certain position upon focusing the control, then do that in such an event handler:

  • for keyboard use OnEnter:
    procedure TForm1.MaskEdit1Enter(Sender: TObject);
    begin
       (Sender as TMaskEdit).SelStart:= 1;  // Second position
    end;
    
  • and for mouse use OnClick with the same code.

It even works unbound to how the property .AutoSelect is set.

Using Backspace is the worst choice input wise, as it always deletes potential content and needs to be pressed several times to go to the first position. Why not using the Home key instead?

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
  • "Just like anything that bases on TEdit putting the focus onto the control via keyboard or mouse will by default put the text cursor at the end of its content." That's not accurate: If you set focus to an edit control by clicking it, the text caret is placed below the mouse cursor. If you focus an edit control using keyboard, all characters are selected. – Andreas Rejbrand Dec 07 '22 at 18:12
  • @AndreasRejbrand Using the keyboard it depends as per `.AutoSelect`; using the mouse I assumed clicking off the text. – AmigoJack Dec 07 '22 at 19:06
  • It's a method that really works on OnClick, but on OnEnter it doesn't work. I tried in several maskEdits, but it still doesn't work. I didn't want to leave it on OnClick for usability, it would bother the user to go back to the beginning whenever he clicks on the component... – Erick Landim Dec 07 '22 at 19:51
  • I tested it successfully on D7: pressing **Tab** to cycle between controls the text cursor was always at the second position. What means "_it doesn't work_" exactly for you? – AmigoJack Dec 07 '22 at 21:12
  • If I use the tab to move between components, it works when I get to the phone component. However, it is necessary that when you click on it with the mouse, it sends it to the first position. This does not happen, clicking with the mouse, even having the code that you sent inside the onEnter, it continues to send it to the last position on the right. – Erick Landim Dec 07 '22 at 21:36
  • Is there any functionality in delphi that you can write the values ​​in maskedit from right to left? I think you can even use the image I attached as an example. You add a value in the last position on the right and it passes the character that you placed one place to the left. So it wouldn't matter where you started writing from. it would also work for what I'm doing. – Erick Landim Dec 07 '22 at 21:39
  • 1
    For mouse use the `onClick` event - I updated the answer. Mouses click, keyboards enter. – AmigoJack Dec 07 '22 at 22:02
  • Everything went well with onClick. Thank you very much! – Erick Landim Dec 08 '22 at 12:04