Using RadTimePicker Class for overriding SelectedDate Property

4 posts, 1 answers
  1. nitesh monga
    nitesh monga avatar
    28 posts
    Member since:
    Mar 2006

    Posted 07 Jul 2009 Link to this post

    Hi there,

    I am trying to create a custom class which inherits radtimepicker class, and overrides the SelectedDate property of the class. but i am stuck at how to use this class with the radtimepicker control and let that control use the override property.

    why i want to do this is because we have lots for classic RadControls in our application now we are migrating them to the new ajax version, previously we were using asp:TextBox with JS to get the time value and to validate the value, with radtimepicker we get all the functionality we want without any extra JS :). the only problem is getting only the time value from the radtimepicker and not the date with that. while searching i found i can do that by using CType(radtimepicker .SelectedDate, DateTime).ToString("tt") but this will take a lot of time to implement :(. if i can just override the SelectedDate property and place the above .ToString("tt") with the GETTER it will be more easy :).

    i hope i am able to explain my problem. any example code will be a great help.

    Thanks
    Nitesh Monga

  2. Veli
    Admin
    Veli avatar
    2002 posts
    Member since:
    Sep 2012

    Posted 10 Jul 2009 Link to this post

    Hello nitesh,

    DateTime object's ToShortTimeString() probably is exactly what you need. So something like:

    radTimePicker.SelectedDate.Value.ToShortTimeString()  should work for you. If not, here is a sample approach:

    public class CustomRadTimePicker : RadTimePicker 
        public string SelectedTime 
        { 
            get 
            { 
                return this.SelectedDate.Value.ToShortTimeString(); 
            } 
            set 
            { 
                DateTime date; 
                if (DateTime.TryParse(value, out date)) 
                { 
                    this.SelectedDate = date; 
                } 
                else 
                { 
                    throw new Exception("String cannot be parsed to a valid DateTime."); 
                } 
            } 
        } 

    Public Class CustomRadTimePicker 
        Inherits RadTimePicker 
        Public Property SelectedTime() As String 
            Get 
                Return Me.SelectedDate.Value.ToShortTimeString() 
            End Get 
            Set 
                Dim [date] As DateTime 
                If DateTime.TryParse(value, [date]) Then 
                    Me.SelectedDate = [date] 
                Else 
                    Throw New Exception("String cannot be parsed to a valid DateTime."
                End If 
            End Set 
        End Property 
    End Class 


    Sincerely yours,
    Veli
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

  3. nitesh monga
    nitesh monga avatar
    28 posts
    Member since:
    Mar 2006

    Posted 11 Jul 2009 Link to this post

    Thank you for the sample code :). i already tested this code but i am unable to use this class with the rad controls :(. it always give a type cast exception..

    thanks
    Nitesh

  4. Answer
    Veli
    Admin
    Veli avatar
    2002 posts
    Member since:
    Sep 2012

    Posted 13 Jul 2009 Link to this post

    Hello Nitesh,

    What are trying to achieve? Can you share some sample code? Alternatively, try the ToShortTimeString() apporach.

    Regards,
    Veli
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

Back to Top