Time Property Type

3 posts, 0 answers
  1. Matt
    Matt avatar
    4 posts
    Member since:
    May 2015

    Posted 12 Jul 2019 Link to this post

    Hi,

    I've managed to work out most property types but I'm struggling how to have a property in Time format.

    I.e. is there a time editor like RadTimePicker within the propertygrid to enter a specified time?

    Thanks.

  2. Nadya
    Admin
    Nadya avatar
    378 posts

    Posted 16 Jul 2019 Link to this post

    Hi Matthew,

    You can subscribe to the EditorInitialized event and get access to the PropertyGridDateTimeEditor. You can use the ItemFormatting event to show only time. Here is an example which result is demonstrated in the attached picture:
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
     
            this.radPropertyGrid1.SelectedObject =  new MyObject() { Time = DateTime.Now, Text = "Time:" };
            this.radPropertyGrid1.EditorInitialized += RadPropertyGrid1_EditorInitialized;
            this.radPropertyGrid1.ItemFormatting += RadPropertyGrid1_ItemFormatting;
        }
     
        private void RadPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
        {
            if (e.Item.Name == "Time")
            {
                var value = (DateTime)(e.Item as PropertyGridItem).Value;
                var element = e.VisualElement as PropertyGridItemElement;
                if (element != null)
                {
                   element.ValueElement.Text = value.ToShortTimeString();
                }
            }
        }
     
        private void RadPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {
            if (e.Item.Name == "Time")
            {
                var editor = e.Editor as PropertyGridDateTimeEditor;
                var element = editor.EditorElement as BaseDateTimeEditorElement;
                element.Format = DateTimePickerFormat.Custom;
                element.CustomFormat = "hh:mm";
                element.ShowTimePicker = true;
                element.Calendar.Visible = false;
            }
        }
    }

    class MyObject
    {
        public string Text { get; set; }
        public DateTime Time { get; set; }
    }

    Please refer to the following help articles for more information about customizing the items appearance and customizing the editor behavior in the RadPropertyGrid:
    https://docs.telerik.com/devtools/winforms/controls/propertygrid/customizing-appearance/customization
    https://docs.telerik.com/devtools/winforms/controls/propertygrid/editors/customizing-editor-behavior

    I hope this information helps. Should you have any other questions, I will be glad to help.

    Regards,
    Nadya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Matt
    Matt avatar
    4 posts
    Member since:
    May 2015

    Posted 16 Jul 2019 Link to this post

    That's fantastic, thank you very much!
Back to Top