I have a custom control with a property that holds the name (full path) to a file location that exists on the target computer.
The exact path will vary according to type of target pc and is typically set right after I add the custom control to my Form, while I am still in design mode of my project, so that when my application runs, it picks up the filename from the property.
It would be convenient if the property opened a file dialog to let me browse to the location (similar to how dialogs are opened when browsing for image and color properties), but this doesn't seem to be possible in visual basic.
After googling for days I have found a couple of articles that touch the subject for other programming languages (see example snippet below) but I haven't been able to work out how to make it work for visual basic.
Here is a snippet I found that mentions the use of an editor, which may be a clue to get started.
[Editor(typeof(FileSelectorTypeEditor), typeof(UITypeEditor))]
public string Filename
{
get { return _filename; }
set { _filename = value; }
}
Hope someone out there can lead me in the right way.
FileSelectorTypeEditoris probably a custom class derived from either FileNameEditor or FolderNameEditor.You can implement both, using the standard class or extend the default with your own, as you have seen in those C# sources you have found.
Here I'm using a specialized
FileNameEditorclass, named (with some lack of imagination)SpecializedFileNameEditorand the standardFolderNameEditorassigning theUITypeEditorto two properties of a class.► The
ImagePathproperty editor is theSpecializedFileNameEditorobject, which uses an OpenFileDialog, where a filter is pre-selected. It also overrides theEditValuemethod, to set the current value, if any, of an associated property (here,ImagePath) as the InitialDirectory of the OpenFileDialog.► The
ImageFolderproperty editor is a standardFolderNameEditor, which opens a FolderBrowserDialog.I'm also attaching an ExpandableObjectConverter type converter, so you can present the two properties as an expandable property selector in a PropertyGrid.
You can see an example here:
How to bind child Controls of a User Control to a Public Property