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

Time Property Type

2 Answers 140 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Matt
Top achievements
Rank 2
Matt asked on 12 Jul 2019, 01:40 PM

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 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 16 Jul 2019, 08:21 AM
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.
0
Matt
Top achievements
Rank 2
answered on 16 Jul 2019, 11:47 AM
That's fantastic, thank you very much!
Tags
PropertyGrid
Asked by
Matt
Top achievements
Rank 2
Answers by
Nadya | Tech Support Engineer
Telerik team
Matt
Top achievements
Rank 2
Share this question
or