简体   繁体   中英

How can I check if the user has entered a value into a TDateTimePicker?

I'm using a VCL Forms application in Delphi 10.3. I'm using a TDateTimePicker to record the date and time.

What type of variable must I save the value into, and how do I check that the user has actually entered a value instead of leaving it blank?

What type of variable must I save the value into [...]?

This is something you can easily find out yourself using the IDE. Indeed, when you type the name of the date time picker control and a period, Code Insight lets you explore the control's properties and methods. Your first attempt will be something about Date , and there you go:

Code Insight 的屏幕截图向我们展示了 TDateTimePicker 的一些属性

I don't know if you use your control to pick only a date or a datetime value, but in either case there is a good choice here: DateTime or Date . I'll assume you want the date only; it makes no difference for my A.

But now you think to yourself, "this Date property seems to be precisely the thing I am looking for, but how can I be sure it really does contain the date displayed in the control (and not, say, the date the app was compiled)?"

Then you close the Code Insight list, place the caret inside the Date identifier, and press F1, which displays the help :

Indicates the date that is marked on the calendar.

Bingo!

But what type should the variable have? Well, both Code Insight and the documentation tells you this: TDate .

[H]ow do I check that the user has actually entered a value instead of leaving it blank?

Well, actually, a TDateTimePicker cannot be blank.

So your actual question is probably, "How can I get notified when the user changes the date?"

This is also something you can discover in the IDE. Select the date time picker in the form designer and go to the Events tab in the Object Inspector :

RAD Studio IDE 中对象检查器的屏幕截图,显示了 TDateTimePicker 控件的事件。

I'll leave it as an exercise for you to figure out which event to use. Also use the F1 key when an event is selected.

But using such an event is a horrible idea. I suppose you want to use it to enable a Next or Submit button. Don't do that. If the initial date happens to be the one the user wants to submit, they will be highly annoyed that they cannot continue without changing the date and then changing it back (if they even manage to figure out that is what they need to do).

What type of variable must I save the value into

TDateTimePicker has Date , Time , and DateTime properties, which return TDate , TTime , and TDateTime values, respectively.

how do I check that the user has actually entered a value instead of leaving it blank?

By default, a TDateTimePicker can't be "blank". Its Date / Time / DateTime properties will always return some value.

If you want the user to be able to specify a non-value, TDateTimePicker has a ShowCheckbox property for that purpose. Set it to True , and then you can query the Checked property before using the Date / Time / DateTime values.

I just need a way to check if the user has made a change at all.

If you just want to know whether the user has changed the value from its default, you can use the OnChange event for that purpose. For example, declare a boolean variable (or, just use the Tag property), set it to False initially, and then have OnChange set it to True , and then you can check that value whenever needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM