Text date input after a null value

4 posts, 1 answers
  1. dave
    dave avatar
    10 posts
    Member since:
    Mar 2011

    Posted 29 Apr 2011 Link to this post

    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
  2. Answer
    Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 04 May 2011 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. dave
    dave avatar
    10 posts
    Member since:
    Mar 2011

    Posted 06 May 2011 Link to this post

    Thanks for the suggestion!  That's a perfect approach and I've got it working the way I want.

    DaveL
  5. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 10 May 2011 Link to this post

    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.
Back to Top