Delphi TAdvDateTimePicker空白默认值 [英] Delphi TAdvDateTimePicker blank default value

查看:158
本文介绍了Delphi TAdvDateTimePicker空白默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个TAdvDateTimePicker组件的表单,以及少量其他编辑(在delphi XE7中)。这两个日期时间字段都是可选的,因此如果用户不想填写它们,则数据库中的两个日期时间字段都将为空。

I have a form with two TAdvDateTimePicker components, and few other edits (in delphi XE7). Both datetime fields are optional, so if the user don't want to fill them, in the database will both be null.

问题是,与例如好旧的TDateEdit,TAdvDateTimePicker即使默认情况下也不接受空白值。我知道,不存在空日期时间之类的东西,但是我需要知道用户是否愿意填写这些日期。因此,将今天的日期保留为默认值,并使用onChange,onEnter或其他任何操作来检查是否已设置值的方法都不是解决方案-他可能想使用今天的日期,所以他就这样保留了它。

Problem is, that unlike for example good old TDateEdit, the TAdvDateTimePicker does not accept a blank value even as default. I know, that something like "null datetime" does not exist, but I need to know, if user did or did not want to fill those dates. So leaving today's date as default, and using onChange, onEnter or whatever action to check if a value has been set, isn't a solution - he may want to use today's date, so he leaves it like that.

当然有可能将默认值设置为1.1.1900(很明显的废话),然后检查其是否已更改,但这是我最后要说的去做。有更好的方法吗?

There is of course a possibility to set the default value to, let's say 1.1.1900 (an obvious nonsense) and then check if it has changed, but that is the last thing I would like to do. Is there a better way?

谢谢

推荐答案

感谢Sertac Akyuz,这是最简单的解决方案:

With thanks to Sertac Akyuz, here is the simplest solution:

on激活表格,将.Format属性设置为空格(''):

onActivate of the form, set .Format property to a space (' '):

procedure TfrmDeliveriesEdit.FormActivate(Sender: TObject);
begin
  DateTimePicker1.Format:= ' ';
end;

然后,在选择器的onEnter处,将格式设置为空字符串('')以返回,如果用户想填写实际值

Then, onEnter of the picker, set the format to empty string ('') to revert back, if the user wants to fill a real value

procedure TfrmDeliveriesEdit.DateTimePicker1Enter(Sender: TObject);
begin
  DateTimePicker1.Format:= '';
end;

编辑:
返回 null值,检查onKeyDown事件中的删除键像这样:

to return back to "null" value, check delete key in onKeyDown event like this:

procedure TfrmDeliveriesEdit.dpTaxDateKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_DELETE then dpTaxDate.Format:= ' ';
end;

这篇关于Delphi TAdvDateTimePicker空白默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆