StringDictionaryEditor & PropertyGrid

S

Sebastien Lange

Hi,

I'd like to display a StringDictionaryEditor in the PropertyGrid, but I
can't get it out! See hereafter the code I'm using. Is it possible? How
should I do?

Sebastien

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design",
"System.Drawing.Design.UITypeEditor, System.Drawing")]

public StringDictionary EnvironmentVariables

{

get

{

return this.environmentVariables;

}

set

{

this.environmentVariables = value;

}

}
 
Y

Ying-Shen Yu[MSFT]

Hi Sebastien,

I tested your code snippet in a simple UserControl derived control, however
the string diectionary editor popups properly. Here is my test code , does
it work on your system? If it does not help you to resolve this issue,
please
send me a simple sample project to reproduce this problem by email, I'd
like to take a look at it and try to see if I could figure out the problem.
Thanks!
<code>
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics.Design;
using System.Collections.Specialized;

namespace MySimpleControl
{
public class MyCtl : UserControl
{
private StringDictionary environmentVariables = new StringDictionary();
[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design",
"System.Drawing.Design.UITypeEditor, System.Drawing")
]
public StringDictionary EnvironmentVariables
{
get {return this.environmentVariables;}
set {this.environmentVariables = value;}
}
}
}

</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner 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, "online" should be removed before
sending.
 
S

Sebastien Lange

Thanks,

Your code works fine in design. But not in runtime: add a propertygrid to a
test form, set myCtl as the SelectedObject, and try to edit
EnvironmentVariables property. Looks like the editor attribute is not taken
into account. Any idea?

Regards,
Sebastien
 
Y

Ying-Shen Yu[MSFT]

Hi Sebastien,

I also found it doesn't work in run-time, after some research, I found this
problem is caused by an assembly resolve failer when propertygrid trying to
get the type from the Typename. You need write the full qualified type name
in Editor Attribute if you want to use the StringDictionaryEditor in
run-time, there are two ways to solve this problem:
1. re-write the Editor Attribute definition as :
Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor,System.Design, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"))

Or a bit simpler if you reference the System.Design Assembly
Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(System.Drawing.Design.UITypeEditor))

2. If you would like to use some more flexible way to instantiate this type
(e.g. for different versions)
You may handle the AppDomain.AssemblyResolve event and provide your own
assembly resolve implementation, here is skeleton for this:
//keep the Editor Attribute as previous
//Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design",
//"System.Drawing.Design.UITypeEditor,System.Drawing")
//AppDomain.CurrentDomain.AssemblyResolve +=new
ResolveEventHandler(CurrentDomain_AssemblyResolve);
private static Assembly CurrentDomain_AssemblyResolve(object sender,
ResolveEventArgs args)
{
if (args.Name == "System.Design")
{
Assembly asm = Assembly.Load("System.Design, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
return asm;
}
if (args.Name == "System.Drawing")
{
Assembly asm = Assembly.Load("System.Drawing, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
return asm;
}
return null;
}
Does it work for you?
Please let me know if you still have problem on it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner 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, "online" should be removed before
sending.
 
S

Sebastien Lange

One last thing. How can I be notified than the Collection changed?
If I put a breakpoint in the set accessor, it is never called. I'd like to
know when the collection changes... what are the solution?

thanks,
Sebastien

Sebastien Lange said:
Thanks a lot, it's working fine!

"Ying-Shen Yu[MSFT]" said:
Hi Sebastien,

I also found it doesn't work in run-time, after some research, I found this
problem is caused by an assembly resolve failer when propertygrid trying to
get the type from the Typename. You need write the full qualified type name
in Editor Attribute if you want to use the StringDictionaryEditor in
run-time, there are two ways to solve this problem:
1. re-write the Editor Attribute definition as :
Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Drawing.Design.UITypeEditor,System.Design, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"))

Or a bit simpler if you reference the System.Design Assembly
Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(System.Drawing.Design.UITypeEditor))

2. If you would like to use some more flexible way to instantiate this type
(e.g. for different versions)
You may handle the AppDomain.AssemblyResolve event and provide your own
assembly resolve implementation, here is skeleton for this:
//keep the Editor Attribute as previous
//Editor("System.Diagnostics.Design.StringDictionaryEditor, System.Design",
//"System.Drawing.Design.UITypeEditor,System.Drawing")
//AppDomain.CurrentDomain.AssemblyResolve +=new
ResolveEventHandler(CurrentDomain_AssemblyResolve);
private static Assembly CurrentDomain_AssemblyResolve(object sender,
ResolveEventArgs args)
{
if (args.Name == "System.Design")
{
Assembly asm = Assembly.Load("System.Design, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
return asm;
}
if (args.Name == "System.Drawing")
{
Assembly asm = Assembly.Load("System.Drawing, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
return asm;
}
return null;
}
Does it work for you?
Please let me know if you still have problem on it.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner 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, "online" should be removed before
sending.
 
Y

Ying-Shen Yu[MSFT]

Actually, if it is a Collection Property, you needn't define a setter for
it, since the CollectionEditor will operate on the collection via IList
Interface, setter will never be used. So if you need get notified when the
collection changes, you may derive the StringDictionary define an event in
it, then fire it in those methods which changes the Collection.

HTH/

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner 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, "online" should be removed before
sending.
 

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