Can I use Edit Mask to format the output? (not just input validation)

Delphi 7 question. I work with a form that has many data controls (change is not an option). I have a TDBEDIT control associated with a TStringField (which has an EditMask property). I know that I can use this EditMask to force the control to check its input, but I want to know if I can fill the field directly with a raw value and display it in accordance with EditMask?

I want to fill in the field with a 16-digit number, but I want it to be displayed as 4 lots of 4 digits (for example, 9999 9999 9999 9999).

If I...

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

I get only the first 4 digits.

I hope there is someone there who is more familiar with the insides of old data controls.

+4
source share
1 answer

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

Since you have TStringField numbers, you have two options:

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

Edit:

Sam used this approach:

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

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

and OnGetText were 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 great. Thanks.

+3
source

Source: https://habr.com/ru/post/1347494/


All Articles