开发者

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

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.E开发者_如何学编程dit;
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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜