Recent

Author Topic: edit fotmat?  (Read 2629 times)

Delphilearner

  • New Member
  • *
  • Posts: 26
edit fotmat?
« on: February 07, 2016, 11:09:09 pm »
hello

how can i change edit to this format 00154

i mean when i enter a number like 154 it will be 00154
thank you in advence

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: edit fotmat?
« Reply #1 on: February 07, 2016, 11:46:48 pm »
At what time should this transfromation happen?

Bart

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
Re: edit fotmat?
« Reply #2 on: February 08, 2016, 04:06:43 pm »
You could probably do it with TEdit's OnExit event, then use Format() to change the formatting. Alternatively, TSpinEdit could possibly do the trick to, or the TMaskEdit.
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Delphilearner

  • New Member
  • *
  • Posts: 26
Re: edit fotmat?
« Reply #3 on: February 08, 2016, 07:40:46 pm »
At what time should this transfromation happen?

Bart

thank you for your reply

i want it --> onchange

You could probably do it with TEdit's OnExit event, then use Format() to change the formatting. Alternatively, TSpinEdit could possibly do the trick to, or the TMaskEdit.

how can i do it i want an example

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: edit fotmat?
« Reply #4 on: February 08, 2016, 09:59:36 pm »
I recommend you OnEditingDone, change will be done when you press Enter inside Edit or when Edit will lost focus (Tab key or mouse).
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1EditingDone(Sender: TObject);
  2. begin
  3.   Edit1.Text:=Format('%.5d', [strtoint(Edit1.Text)]);
  4. end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: edit fotmat?
« Reply #5 on: February 08, 2016, 10:00:49 pm »
I would not do it in OnChange because you will have a very hard time entering or editing a text becauses with every key input the missing zeros will be added and you have to be very careful to keep track of the cursor.

OnExit or OnEditingDone work much better, in my opinon; here the zeros are added whenever the edit loses focus. And I personally would prefer to edit the unmodified text. Therefore the OnEnter event should remove the added zeros again:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Enter(Sender: TObject);
  2. var
  3.   s: String;
  4. begin
  5.   s := Edit1.Text;
  6.   while (Length(s) > 0) and (s[1] = '0') do
  7.     Delete(s, 1, 1);
  8.   Edit1.Text := s;
  9.   Edit1.SelectAll;
  10. end;
  11.  
  12. procedure TForm1.Edit1Exit(Sender: TObject);
  13. var
  14.   s: String;
  15. begin
  16.   s := Edit1.Text;
  17.   while (Length(s) < 6) do s := '0' + s;
  18.   Edit1.Text := s;
  19. end;

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: edit fotmat?
« Reply #6 on: February 08, 2016, 10:17:36 pm »
... or the TMaskEdit.

Take a TMaskEdit with EditMask := '00000;0;0'.
Then, when you need the content, do not use Text property, but EditText property.
It will always consist of 5 numbers, and the user cannot delete the zero's.

Bart

 

TinyPortal © 2005-2018