I want to use "Select resource" dialog in my dropdown UITypeEditor for custom struct property.
I already have TestEditorControl:UserControl
, which contains a button1
with event handler for Click
:
btn.Click+=(s,a)=>{
OpenFileDialog oDlg = new OpenFileDialog();
if (oDlg.ShowDialog() == DialogResult.OK)
{
...
}
}
How to replace "OpenFileDialog" with "Select resource" dialog? I tried this code (based on Visual Studio "Select Resource" dialog replacement):
private Form resDialog;
public TestEditorControl()
{
InitializeComponent();
var property = TypeDescriptor.GetProperties(button1)["Image"];
var resourceEditorSwitch = property.GetEditor(typeof(UITypeEditor)) as UITypeEditor;
var editorToUseField = resourceEditorSwitch.GetType().GetProperty("EditorToUse",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
var editorToUse = editorToUseField.GetValue(resourceEditorSwitch);
//System.NullReferenceException (editorToUseField == null)
var resourcePickerUIField = editorToUse.GetType().GetField("resourcePickerUI",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
var resDialog= (Form)Activator.CreateInstance(resourcePickerUIField.FieldType);
}
btn.Click+=(s,a)=>{
if (resDialog.ShowDialog() == DialogResult.OK)
{
...
}
}
To show editor of a property, you need to get the
UITypeEditor
of the property and call itsEditValue
:The values you need to pass to the method, depend to the context of the code, in addition to the example in this post, I've shared a few other links at bottom of this post.
Example - Show Select Image dialog for a nested property
In this example I have created a
MyTestControl
, which has a property calledMyTestProperty
which is ofMyTestClass
type, which has aMyTestImage
property ofImage
type. I'm going to show aUITypeEditor
forMyTestProperty
, and a button inside the editor which shows select image dialog and changes the image property:The Control
UITypeEditor
Editor control
TypeDescriptionContext
More example
You may want to look into following posts for more example: