locked
DefaultValue not recognized by designer RRS feed

  • Question

  • I am working on a custom button class, and am having a miserable time tryying to get the form designer to recognize the default values for some of the custom properties. The app is being developed in VS2008 (we need NetCF 3.5 for the WCF stuff) but I can duplicate the problem in 2005 quite easily. I am aware that DefaultValueAttribute does not actually set the property; the problem I'm having is that the designer doesn't recognize when the property is set to it's default value; it remains boldfaced in the property grid and continues to be serialized in the form's *.designer.cs file.

    Scaling things down to the simplest example I can think of, I have the following:

    A custom control, with a string property named Foo:

    Code Block

    public partial class TestControl : Control
        {
            private string _foo = "Test";

            [DefaultValue("Test")]
            public string Foo
            {
                get { return _foo; }
                set { _foo = value; }
            }

            public TestControl()
            {
                InitializeComponent();
            }

            protected override void OnClick(EventArgs e)
            {
                MessageBox.Show("Value is " + _foo);
                base.OnClick(e);
            }

            protected override void OnPaint(PaintEventArgs pe)
            {
                // TODO: Add custom paint code here

                // Calling the base class OnPaint
                base.OnPaint(pe);
            }
        }


    If I create a form and drag the control from the toolbox onto the form, Foo appears in the Misc section with a value of "Test" and is boldfaced. If I change the value and then right-click, the Reset command is dimmed.

    Creating a xmta file and putting the default value there also does nothing:

    Code Block

    <?xml version="1.0" encoding="utf-16"?>
    <Classes xmlns="http://schemas.microsoft.com/VisualStudio/2004/03/SmartDevices/XMTA.xsd">
      <Class Name="Controls.TestControl">
        <Property Name="Foo">
          <DefaultValue>
            <Type>System.String</Type>
            <Value>Test</Value>
          </DefaultValue>
          <Category>Test</Category>
        </Property>
      </Class>
    </Classes>


    In this case, the category is honored (Foo moves from Misc to Test) but the default value still doesn't work.

    Any ideas?
    Friday, October 5, 2007 6:25 AM

Answers

  •  

    Sounds like a bug - If I try to put your simple example on a clean windowsapplication (VS2005), it works without any problem.
    Friday, October 5, 2007 10:11 AM

All replies

  •  

    Sounds like a bug - If I try to put your simple example on a clean windowsapplication (VS2005), it works without any problem.
    Friday, October 5, 2007 10:11 AM
  • I'm working with a NetCF app here; it seems the designer for PPC forms is a tad brain-dead in some ways compared to the normal Winforms designer.

    Further experimentation has shown that changing the control's base class from Control to UserControl (or Button, or something else) enables properties with default values defined in XMTA (but not with a DefaultValue attribute) to work correctly, provided the type is built-in. Custom types/enums still don't work even when defined in XMTA, but ShouldSerializeXXX and ResetXXX work fine for the enum property in my project, although they didn't work when I tried them with Control as the base class.
    Friday, October 5, 2007 7:34 PM
  • I just tried your snippet and it works for me (both VS2005 and VS2008). The 'Test' value of "Foo" is not bold initially, but after I changed the property to 'Test2', then RightClick and 'Reset' the property is again 'Test'.

    Monday, October 8, 2007 3:26 AM
  • Hello,

     

    I have the same problem. Funny thing is that it works for the Width property, but not for Height or Size.

    If anyone knows how to overcome this issue...

     

    Marthijn

    Monday, January 28, 2008 12:53 PM