Custom StringArrayEditor

S

Sebastien Lange

Hi,

I want to edit a property (type=string[]) in the propertygrid. Usually you
use StringArrayEditor.
But I'd like to change the instruction displayed in the StringCollectionForm
and the text of the form.
Unfortunaltely, StringArrayEditor is internal, so I can't inherit.
I tried to inherit from CollectionEditor and then provide
StringCollectionForm (see hereafter). So I can change the instruction and
text (thanks to reflection). But if I change the collection (add a string, I
get an 'object reference not set to...' outside of my code.
Do you see a better way to do it? What's wrong? I know I can create my own
MyStringCollectionForm, but I don't want to do that.

Any idea?

Sebastien

public class TheObject{

[Editor(typeof(OutputArrayEditor), typeof(UITypeEditor))]
public string[] TheProperty{
...
}
}

public class OutputArrayEditor : CollectionEditor //ideal would be
StringArrayEditor
{
public OutputArrayEditor(Type type) : base (type){}

protected override
System.ComponentModel.Design.CollectionEditor.CollectionForm
CreateCollectionForm()
{
Type editorType =
Type.GetType("System.Windows.Forms.Design.StringArrayEditor, System.Design,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
object editor = editorType.InvokeMember(null, BindingFlags.CreateInstance,
null, null, new object[]{typeof(string[])});
Form form = (Form)editorType.InvokeMember("CreateCollectionForm",
BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Instance,
null, editor, null);
form.Text = "My Custom collection text";
//add code to chg instruction here (reflection)
return (CollectionEditor.CollectionForm)form;//base.CreateCollectionForm
();
}

protected override object SetItems(object editValue, object[] value)
{
return base.SetItems (editValue, value);
}

protected override object[] GetItems(object editValue)
{
return base.GetItems (editValue);
}
}
 
Y

Ying-Shen Yu[MSFT]

Hi Sebastien,

I traced the code for some time and found the exception was caused by a
null reference of the Context property in StringArrayEditor object (editor).
You need set the Context value for the string array editor in EditValue
method, it might be set using reflection again before calling the
base.EditValue of your OutputArrayEditor class.

But since you are using private reflection I can't promise the code will
work without problem. For this issue , I think it would be better to write
your own String Array editor instead of private reflection.

Thanks for your understanding!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
E

Emad Nebih

Hi,

Can u provide me please with how you created the context once it is
null. I am having the same problem. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top