Recent

Author Topic: Tfield EditMask  (Read 6690 times)

Walmir

  • New Member
  • *
  • Posts: 39
Tfield EditMask
« on: August 01, 2014, 09:36:09 pm »
Hi,
I need to use various masks with string fields. Some of them are fairly complex, like 00.000.000/0000-00;0;_
For some reason, I only can see the masks when I edit the field. They disappear as soon as the focus leaves the data aware control.
I tried to use OnGetText but it seems that MaskUtils FormatMaskText has some difficulties with masks that end with ";0;_".

Lazarus 1.2.4
Windows 7

Walmir

  • New Member
  • *
  • Posts: 39
Re: Tfield EditMask
« Reply #1 on: August 07, 2014, 02:21:30 pm »
No one can help me with this?

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Tfield EditMask
« Reply #2 on: August 07, 2014, 02:23:23 pm »
You can post a full compilable project that demonstrates the problem as an attachment (zip file).
Others may then be more motivated to help you and even if you don't get help, you can use it when reporting a bug in the bugtracker.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Tfield EditMask
« Reply #3 on: August 09, 2014, 02:10:12 am »
An EditMask of a TField is used only when editing that field.

For a TNumericField you could use the DisplayFormat property.
For other field-types you need to write an OnSetText and OnGetText for the field.

You can read about how to do this here:
http://stackoverflow.com/questions/5617205/can-i-use-an-edit-mask-to-format-output-not-just-validate-input

I tried to use OnGetText but it seems that MaskUtils FormatMaskText has some difficulties with masks that end with ";0;_".
You need to show us that code so we could see why it's not working.

Walmir

  • New Member
  • *
  • Posts: 39
Re: Tfield EditMask
« Reply #4 on: August 09, 2014, 05:37:34 pm »
An EditMask of a TField is used only when editing that field.

I was not aware of this. In Delphi I used TField EditMask both to display and edit.

For a TNumericField you could use the DisplayFormat property.
For other field-types you need to write an OnSetText and OnGetText for the field.

You can read about how to do this here:
http://stackoverflow.com/questions/5617205/can-i-use-an-edit-mask-to-format-output-not-just-validate-input

As I said, I tried to use OnGetText but it seems that MaskUtils FormatMaskText has some difficulties with masks that end with ";0;_".
No serious developer will store the mask in the database, as in "(99)9999-999;1;_". In this way, the masks works fine (sure!).

You need to show us that code so we could see why it's not working.

Please see my previous post.

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Tfield EditMask
« Reply #5 on: August 09, 2014, 06:08:58 pm »
You need to show us that code so we could see why it's not working.

Please see my previous post.
I can't find anywhere in your code where you tried the onGetText as suggested.
Can you point it out?

For example... the following works fine for me:
Code: [Select]
procedure TdmData.qryFornCNPJGetText(Sender: TField; var aText: string;
  DisplayText: boolean);
begin
  aText := TStringField(Sender).Value;
  if DisplayText then // when displaying the text do this
    aText := FormatMaskText('00\.000\.000\/0000\-00;0;_', aText);
end;

Edit:
You can do it even more universal like this:
Code: [Select]
procedure TdmData.qryFornCNPJGetText(Sender: TField; var aText: string;
  DisplayText: boolean);
begin
  aText := Sender.AsString;
   if DisplayText and (aText <> '') and (Sender.EditMask <> '') then
     aText := FormatMaskText(Sender.EditMask, aText);
end;
So there is no need to copy the EditMask to your code.
« Last Edit: August 09, 2014, 11:38:11 pm by rvk »

Walmir

  • New Member
  • *
  • Posts: 39
Re: Tfield EditMask
« Reply #6 on: August 11, 2014, 07:07:19 pm »
Sorry,
I removed OnGetText from my code because I thoght there was a bug. But the real problem was that I was trying to use OnGetText both to display the mask and edit it, not even testing DisplayText.

Walmir

  • New Member
  • *
  • Posts: 39
Re: Tfield EditMask
« Reply #7 on: August 11, 2014, 07:16:01 pm »
Ok,
So I will use OnGetText to display fields that have the property EditMassk set.
Will I also need an OnSetText event on these fields?


rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Tfield EditMask
« Reply #8 on: August 11, 2014, 08:21:23 pm »
Will I also need an OnSetText event on these fields?
You could test this yourself...  ;)
But no, you don't need the OnSetText.

The field-value itself stays "clean" without the separators.
Only when testing for DisplayText in OnGetText the separators are added but that value is not used besides for display purposes (if DisplayText is true). Because the field is "clean" you don't need the OnSetText.

 

TinyPortal © 2005-2018