views:

81

answers:

3

How can I validate that a value has been selected in a datetimepicker control.

The control is bound to a bindingnavigater so when the save button is clicked I want to ensure a date has been selected?

+1  A: 

Listen for the ValueChanged event, then set a boolean flag in it.

SLaks
A: 

I would rather validate if the value makes sense (from your system's point of view), not if it was physically entered.

Grzenio
A: 

The easiest way to do this is to make a subclass of BindingNagivator which allows you to subscribe to the Validating event, and handle your validation, preventing the move.

A simple implementation is shown in this forum post (ValidatingBindingNavigator).

This allows you to validate prior to allowing a change in movement, or a save.

Reed Copsey
Will this also prevent the ( + ) add new from creating a new record (on form) unless the validation is true?
db83
Yes. It overrides any button press, since it's using WndProc to handle left button down. Basically, none of the buttons work unless validating is uncanceled. It's an easy way to handle it. (There are other alternatives in that same post, but they show disabling the buttons, which is a bit more work).
Reed Copsey
Ok cool. The goal is to use this in conjunction with error provider so if the user forgets to enter a field or enters valid data then clicks the save or create new then the errors will show next to the relevant controls. I'll try to implement this and come back with results. Thanks
db83
"valid" should read invalid
db83
This should work great, then. This will prevent the user from moving - just have validate cancel if this.Errors.Count > 0 or this.Error != string.Empty.
Reed Copsey