This is a migrated thread and some comments may be shown as answers.

Text date input after a null value

3 Answers 74 Views
Calendar, DateTimePicker, TimePicker and Clock
This is a migrated thread and some comments may be shown as answers.
dave
Top achievements
Rank 1
dave asked on 29 Apr 2011, 05:19 PM
I'm attempting to get a RadDateTimePicker to allow "friendly" input using either the calendar or the text box.  The situation is that the current value of the picker is the null value.  It correctly displays the NullText in the text box.  When the calendar drop down is opened, it originally display the NullDate, but I've got that changed to display the current date by setting the calendar.FocusedDate property which is described in another thread.

However, I can't figure out how to get a similar behavior when typing in a date value.  The user tabs into the control.  The NullText is removed and an insertion point is left in the empty text box (correct).  When the user types the first digit, the text box is populated with the NullDate modified by the digit that was typed.

I'd like to have today's date populate the text box instead of the NullDate.  I've tried setting the text box Text property from various event handlers, but it's always overwritten with the NullDate.  What do I need to set and when do I need to set it?

Thanks,
DaveL

3 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Todorov
Telerik team
answered on 04 May 2011, 05:00 PM
Hello dave,

Thank you for your question.

Below is a code sample that demonstrates how this can be achieved. The basic idea is to change the value of the date picker when it receives/looses focus and stop changing it when the user has selected a date.
public partial class Form1 : Form
{
    private bool dateChanged = false;
    private DateTime nullDate;
 
    public Form1()
    {
        InitializeComponent();
 
        nullDate = this.radDateTimePicker1.NullDate;
        this.radDateTimePicker1.Value = nullDate;
        this.radDateTimePicker1.GotFocus += new EventHandler(radDateTimePicker1_GotFocus);
        this.radDateTimePicker1.LostFocus += new EventHandler(radDateTimePicker1_LostFocus);
        this.radDateTimePicker1.ValueChanged += new EventHandler(radDateTimePicker1_ValueChanged);
        this.radDateTimePicker1.KeyDown += new KeyEventHandler(radDateTimePicker1_KeyDown);
    }
 
    void radDateTimePicker1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            this.dateChanged = true;
        }
    }
 
    void radDateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        this.dateChanged = true;
    }
 
    void radDateTimePicker1_LostFocus(object sender, EventArgs e)
    {
        if (this.dateChanged) return;
        this.radDateTimePicker1.ValueChanged -= new EventHandler(radDateTimePicker1_ValueChanged);
        this.radDateTimePicker1.Value = nullDate;
        this.radDateTimePicker1.ValueChanged += new EventHandler(radDateTimePicker1_ValueChanged);
    }
 
    void radDateTimePicker1_GotFocus(object sender, EventArgs e)
    {
        if (this.dateChanged) return;
        this.radDateTimePicker1.ValueChanged -= new EventHandler(radDateTimePicker1_ValueChanged);
        this.radDateTimePicker1.Value = DateTime.Today;
        this.radDateTimePicker1.ValueChanged += new EventHandler(radDateTimePicker1_ValueChanged);
    }
}

I hope this helps. Feel free to ask if you have any additional questions.

Greetings,
Ivan Todorov
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
0
dave
Top achievements
Rank 1
answered on 06 May 2011, 06:53 PM
Thanks for the suggestion!  That's a perfect approach and I've got it working the way I want.

DaveL
0
Ivan Todorov
Telerik team
answered on 10 May 2011, 04:57 PM
Hi dave,

I am glad I could help.

Should you need further help, do not hesitate to contact us.

Kind regards,
Ivan Todorov
the Telerik team
Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
dave
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
dave
Top achievements
Rank 1
Share this question
or