2014-11-03 19 views
0

我已經將這個TEditMask當字段有EditMask時如何允許空的TDBEdit?

DateTimeField.EditMask := '!99/99/9999;1; '; 

我有鏈接到這個領域的TDBEdit

該掩碼適用於有效日期,但當用戶嘗試清除輸入值(使用DEL鍵)時,離開該掩碼時將導致EConvertError

如何更改此行爲來清除字段值?

+0

而是清除底層字段。 'DateTimeField.Clear'。我通常會提供一個按鈕來清除這些字段(例如'TButtonedEdit'中的一個或者我們在內部編寫的一個可以識別DB的字段,但是普通的TButton或TSpeedButton也可以。 – 2014-11-03 19:47:58

+0

這是一個很好的解決方法。 – 2014-11-03 20:00:18

回答

2

我發現使用OnSetText事件領域的解決方案:

procedure TForm1.DateTimeFieldSetText(Field: TField; const Text: string); 
begin 
    // This only works for locales where the date separator is '.' 
    if Text = ' . . ' then 
    Field.AsString := '' 
    else 
    Field.AsString := Text 
end; 
2

您可以測試在基礎數據字段的OnSetText事件處理程序對一個空濛版編輯文本,並設置字段值只有在沒有匹配的情況下。

procedure TForm1.DataTimeFieldSetText(Sender: TField; const Text: string); 
begin 
    if not (maskutils.FormatMaskText(Sender.EditMask, '') = Text) then 
    Sender.AsString := Text; 
end; 

您不必知道編輯掩碼或特定區域設置信息。

相關問題