Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / June 2004

Tip: Looking for answers? Try searching our database.

Resettings Subproperty in PropertyGrid doesn't work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sven Meyer - 27 Apr 2004 21:11 GMT
Hi

My goal is to extent the VS.NET button with some more custom
properties. I've created an inherented control which exposes a custom
class containing these new (sub)properties.

I ran into the following problem:

If I set the value for a subproperty, it is correctly serialized to
the sourcecode. Everthing is fine. After saving the project, I reset
the subproperty from the contextmenu. The value shown in the
PropertyGrid is resetted, but it is NOT serialized (removed from the
code). Changing the value is possible, only reset doesn't work.

Here is an example. It uses a custom TypeConverter and some
ShouldSerialize/Reset-functions:

public class EButton : Button {
  private Prop m_TestProp = new Prop(null);

  [TypeConverter(typeof(PropConverter))]
  public Prop TestProp {
     get {   return m_TestProp; }
     set {   m_TestProp = value; }
  }

  protected virtual bool ShouldSerializeTestProp() {
     return m_TestProp.Img != null;
  }

  protected virtual void ResetTestProp() {
     this.TestProp = new Prop(null);  //works
  }
}

[TypeConverter(typeof(PropConverter))]
public class Prop {
  public Prop(Image Img) {
     m_Img = Img;
  }

  private Image m_Img;

  [DefaultValue(null), NotifyParentProperty(true),]
  public Image Img {
     get { return m_Img; }
     set { m_Img = value; }
  }

  public void ResetImg(){
     this.Img = null;  // dont work, not serialized
  }
}

public class PropConverter : ExpandableObjectConverter {
  public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType) {
     if (destinationType == typeof(InstanceDescriptor)) return true;

     return base.CanConvertTo(context, destinationType);
  }

  public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType) {
     if (destinationType == typeof(InstanceDescriptor) && value is
Prop) {
        Prop NewProp = (Prop)value;

        ConstructorInfo CInfo = typeof(Prop).GetConstructor(new
Type[] { typeof(Image) });

        if (CInfo != null) return new InstanceDescriptor(CInfo, new
object[] { NewProp.Img });
     }
     return base.ConvertTo(context, culture, value, destinationType);
  }
}

steps:

- assign a new Image to the "TestProp -> Img" subproperty in the
designer
- new value shows up correctly and is serialized to the code
- save the project/sourcefile
- reset the "Img" property from the contextmenu (NOT the whole
"TestProp" property, only the subproperty "Img")
- displayed value for "Img" is properly resetted (null), but this
change wasn't serialized to the sourcecode

Why? What do i miss?

Sven
Eric Cadwell - 28 Apr 2004 21:59 GMT
That's is the function of DefaultValue or ShouldSerialize. It should not
persist "default" values. If it is removing the persistence code from the
class it is working as it should.

If it persisted values for every property there would be thousands of lines
of useless code in the InitializeComponent block.

HTH;
Eric Cadwell
http://www.origincontrols.com
Sven Meyer - 29 Apr 2004 09:01 GMT
Hi Eric

Thanks for your answer.

>If it is removing the persistence code from the
>class it is working as it should.

Thats exactly my problem. Once a value is persisted to code, it is NOT
removed when the subproperty is resetted (after saving the project).
It stays in the code and therefore after I compile the project for
example, the old value is still there (and shown again in the
propertygrid).

The Propertygrid looks like the following:

1. step: initial situation
- TestProp
  Img        (Kein)            (i'm using a german VS.NET)

2. step: after assigning a value to Img
- TestProp
  Img        <some image>

This change is persisted to code. Now, I save the project (important).

3. step: reset the subproperty Img from the contextmenu
- TestProp
  Img        (Kein)

Looks fine in the propertygrid, but if I look at the sourcecode, the
old value is still there. It is not removed from code. i.e.

this.TestButton1.TestProp = new
...Prop((System.Drawing.Bitmap)(resources.GetObject("resource")));

The strange issue is: if the whole TestProp property is resetted from
the contextmenu, it works fine, the line is removed. only when I reset
the subproperty itself, the code isn't removed.

Sven

> That's is the function of DefaultValue or ShouldSerialize. It should not
> persist "default" values. If it is removing the persistence code from the
[quoted text clipped - 6 lines]
> Eric Cadwell
> http://www.origincontrols.com
Keith Hill - 15 Jun 2004 03:11 GMT
Did you ever figure this out? I'm having the exact same problem.

--
Keith

> Hi Eric
>
[quoted text clipped - 36 lines]
>
> Sven
Sven Meyer - 29 Apr 2004 09:01 GMT
Hi Eric

Thanks for your answer.

>If it is removing the persistence code from the
>class it is working as it should.

Thats exactly my problem. Once a value is persisted to code, it is NOT
removed when the subproperty is resetted (after saving the project).
It stays in the code and therefore after I compile the project for
example, the old value is still there (and shown again in the
propertygrid).

The Propertygrid looks like the following:

1. step: initial situation
- TestProp
  Img        (Kein)            (i'm using a german VS.NET)

2. step: after assigning a value to Img
- TestProp
  Img        <some image>

This change is persisted to code. Now, I save the project (important).

3. step: reset the subproperty Img from the contextmenu
- TestProp
  Img        (Kein)

Looks fine in the propertygrid, but if I look at the sourcecode, the
old value is still there. It is not removed from code. i.e.

this.TestButton1.TestProp = new
...Prop((System.Drawing.Bitmap)(resources.GetObject("resource")));

The strange issue is: if the whole TestProp property is resetted from
the contextmenu, it works fine, the line is removed. only when I reset
the subproperty itself, the code isn't removed.

Sven

> That's is the function of DefaultValue or ShouldSerialize. It should not
> persist "default" values. If it is removing the persistence code from the
[quoted text clipped - 6 lines]
> Eric Cadwell
> http://www.origincontrols.com

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.