Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use an Edit Mask to format output? (not just validate input)

Tags:

delphi

Delphi 7 question. I'm working with a form that has many databound controls (changing this is not an option). I have a TDBEDIT control bound to a TStringField (which has a EditMask property). I know I can use this EditMask to force the control to validate its input, but what I want to know is if I can populate the field directly with the raw value and have the control display it according to the EditMask?

I want to populate the field with a 16 digit number, but I want it displayed as 4 lots of 4 digits (ie. 9999 9999 9999 9999).

If I do ...

dbedtABCNumber.DataSource.DataSet.Edit;
dbedtABCNumber.Field.Value := '1234567812345678';

I only get the first 4 digits displayed.

I hope there's someone out there who's more familiar with the intracacies of old databound controls.

like image 844
Sam Avatar asked Oct 12 '22 08:10

Sam


1 Answers

You can use the TField.OnGetText event or TNumericField.DisplayFormat property to modify how the text is being displayed.

Since you have a TStringField holding numbers, you have two choices:

  • use a TNumericField and the DisplayFormat property
  • use the OnGetText event and do your own string formatting

Edit:

Sam used this approach:

I implemented OnSetText and OnGetText event handlers. I already had the Edit Mask 9999 9999 9999 9999;1;_ so the OnSetText was just

TStringField(Sender).Value := Trim(Text);

and OnGetText was just

sValue := TStringField(Sender).Value;  
Text := Format('%s %s %s %s', [Copy(sValue, 1, 4), Copy(sValue, 5, 4), Copy(sValue, 9, 4), Copy(sValue, 13, 4)]);

It works fine. Thanks.

like image 167
Jeroen Wiert Pluimers Avatar answered Oct 14 '22 09:10

Jeroen Wiert Pluimers