DateTimePicker control with custom custom date format

5 posts, 1 answers
  1. John Paul
    John Paul avatar
    2 posts
    Member since:
    Jul 2010

    Posted 04 Oct 2010 Link to this post

    Hi,

    We are currently implementing a control that lets a user specify a date range within our application.  This seems like an ideal use of two DateTimePicker controls (start and end date).

    The problem is that we want the textbox element of this control to display the datetime in a way that does not seem to be supported by the general custom date format characters.

    The company uses a format of YYYYWW (week-week) to represent it's dates and that's the preferred format any date input.

    Is there any way to tell the text element to call one of our methods to convert the date from the calender control rather than the standard DateTime.ToString("custom format") method.

    Thanks
    JP
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 04 Oct 2010 Link to this post

    Hello John,

    I would suggest creating a custom RadDateTimePicker and create an  an FormatProvider event, that would handle the value changed event of the control, in order to change the display text based on one of your methods of date conversion.

    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace TestDateTimePickerCustomFormat
    {
        using System;
        using System.Drawing;
     
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.Load += new EventHandler(Form1_Load);
                this.Size = new Size(200, 60);
            }
     
            void Form1_Load(object sender, EventArgs e)
            {
                var customDateTimePicker1 = new CustomDateTimePicker();
                customDateTimePicker1.Dock = DockStyle.Fill;
     
                this.Controls.Add(customDateTimePicker1);
                customDateTimePicker1.Format = DateTimePickerFormat.Custom;
                customDateTimePicker1.FormatProvider += radDateTimePicker1_FormatProvider;
                customDateTimePicker1.Value = DateTime.Now;
            }
     
            string radDateTimePicker1_FormatProvider(object sender, CustomFormatProviderEventArgs args)
            {
                // get the required string value here
                return args.CurrentDate.ToShortDateString();
            }
        }
     
        public class CustomDateTimePicker : RadDateTimePicker
        {
            public delegate string CustomFormatProviderEventHandler(object sender, CustomFormatProviderEventArgs date);
     
            public event CustomFormatProviderEventHandler FormatProvider;
     
            protected override void OnValueChanged(EventArgs e)
            {
                base.OnValueChanged(e);
     
                if (this.Format == DateTimePickerFormat.Custom && this.FormatProvider != null)
                {
                    this.DateTimePickerElement.TextBoxElement.Text = this.FormatProvider(this, new CustomFormatProviderEventArgs(this.Value));
                }
            }
        }
     
        public class CustomFormatProviderEventArgs : EventArgs
        {
            public DateTime CurrentDate { get; set; }
     
            public CustomFormatProviderEventArgs()
            {
            }
     
            public CustomFormatProviderEventArgs(DateTime dateTime)
            {
                this.CurrentDate = dateTime;
            }
        }
    }

    Hope this will help, please let me know if there is anything else i can help you with.

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. John Paul
    John Paul avatar
    2 posts
    Member since:
    Jul 2010

    Posted 05 Oct 2010 Link to this post

    That worked brilliantly... and seemed obvious in hindsight.

    Thanks for your help.
    JP

  5. Santosh
    Santosh avatar
    1 posts
    Member since:
    Jun 2012

    Posted 09 Oct 2012 Link to this post

    I am using the raddatetimepicker version "2012.2.726.40" in my project.

    My Requirement: I need weekly format to be displayed in RadDateTimePicker control, Example:"8 Oct, 2012 - 14 Oct 2012", the user shoudl be able to change the weeks.

    I tried using the example but it is not working, can you please provide me an example ASAP.

    Thank you,
  6. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 10 Oct 2012 Link to this post

    Hi Santosh,

    This functionality is not supported, because RadDateTimePicker uses the DateTime structure which internally treats days and months as one value. Due to the nature of your request, I am not able to provide you with a proper solution.

    Greetings,
    Peter
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready