I have created a custom UITypeEditor
which launches a form (StringSelector) to display a list of strings which the user choses from. The problem is that this form needs to know what StringManager to use (stringmanage is simply a class which contains all the strings allowed in a List).
When I created this form I was passing in the StringManager as a parameter in the Constructor, but I cannot work out how i can do this with the UITypeEditor.
Below is my current code which uses a constructor which has no parameters, just to get the form to show, however there are obviously no strings as I didn't call the parameter version of the constructor.
How can I pass a parameter to the UITypeEditor which I can then use within the EditValue function? Many thanks.
class StringSelectorEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
IWindowsFormsEditorService svc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
StringItem item = value as StringItem;
if (svc != null)
{
// ###### How do I pass a reference to this EditValue function so that I can....
using (StringSelector form = new StringSelector(/* ... INSERT IT HERE */))
{
form.Value = item;
if (svc.ShowDialog(form) == DialogResult.OK)
{
item = form.Value; // update object
}
}
}
return value; // can also replace the wrapper object here
}
}
UPDATED with additional detail: As requested, I have a class called ControlInstance which itself contains the populated StringManager. It is this ControlInstance Class which is passed to the PropertyGrid control and its accessor functions displayed in it including the above described StringSelectorEditor UITypeEditor reference. Here is a snippet of the code:
public class ControlInstance_Label : ControlInstance
{
StringManager stringManager;
string thisName = "";
StringItem linkedStringItem;
public ControlInstance_Label(String TextFilePath)
{
// Code here which populates the StringManager with text from the above file
}
[Category("Design"), Description("Control Name")]
public String Name
{
get { return thisName; }
set { thisName = value; }
}
// THIS IS WERE I SOMEHOW NEED TO PASS IN THE StringManager Ref to the EditValue function of the custom UITypeEditor
[Category("Design"), Description("Static String Linked to this Control")]
[Editor(typeof(StringSelectorEditor), typeof(UITypeEditor))]
public StringItem LinkedString
{
get { return linkedStringItem; }
set { linkedStringItem = value; }
}
}
See Question&Answers more detail:os