How can I get a file path through propertyGrid?

1 Answer 45 Views
PropertyGrid
Hwanny
Top achievements
Rank 1
Hwanny asked on 01 Dec 2021, 07:32 AM

Hi~!

I am using RadPropertyGrid in WinForm. 

One item of propertygrid is File Path(string). When user click the File Path element to edit, I want to show Openfiledialog.

When I use Windows default control, it works well, but when I use telerik control, it doesn't work.

{

        string _LogFile = "";

        [Category("Design")]
        [Editor(typeof(FileLocationEditor), typeof(System.Drawing.Design.UITypeEditor))]
        [Description("Viewer background image path.")]
        public string LogFileName
        {
            get
            {
                return _LogFile;
            }
            set
            {
                _LogFile = value;
            }
        }
    }

    public class FileLocationEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                // set file filter info here
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    return ofd.FileName;
                }
            }
            return value;
        }
    }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 Dec 2021, 12:28 PM
Hello, Hwanny, 

I have prepared a sample code snippet for your reference demonstrating how to specify the PropertyGridBrowseEditor to be used for a string field that stores a file path: 
        public RadForm1()
        {
            InitializeComponent();
            this.radPropertyGrid1.SelectedObject = new Item(123, "Telerik test", @"C:\Projects");
        }

        public class Item
        {
            public int Id { get; set; }

            public string Name { get; set; }
            
            [Editor(typeof(PropertyGridBrowseEditor), typeof(BaseInputEditor))]
            public string FilePath { get; set; }

            public Item(int id, string name, string filePath)
            {
                this.Id = id;
                this.Name = name;
                this.FilePath = filePath;
            }
        }

An alternative approach is to handle the EditorRequired event and specify the DialogType of the editor as well: 

        private void radPropertyGrid1_EditorRequired(object sender, PropertyGridEditorRequiredEventArgs e)
        {
            if (e.Item.Label=="FilePath")
            {
                PropertyGridBrowseEditor editor = new PropertyGridBrowseEditor();
                RadBrowseEditorElement element = editor.EditorElement as RadBrowseEditorElement;
                element.DialogType = BrowseEditorDialogType.RadOpenFileDialog;
                e.Editor = editor;
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Hwanny
Top achievements
Rank 1
commented on 01 Dec 2021, 12:44 PM

It works well. It helps me a lot.

Thank you very much.

Best regards.

Tags
PropertyGrid
Asked by
Hwanny
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or