Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom properties defined in base form lose their state in inherited form upon rebuild

I am having trouble with properties of a base form not maintaining state in an inherited form.

Environment:

  • Visual Studio 2010 Ultimate Service Pack 1: Version 10.0.40219.1 SP1Rel
  • .Net Framework: Version 4.0.30319 SP1Rel
  • Windows 7 Ultimate

Below, is the source code and steps to reproduce:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test
{
    public partial class BaseForm : Form
    {
        [DefaultValueAttribute(true)]
        public bool ControlVisible
        {
            get
            {
                return this.checkBox1.Visible;
            }
            set
            {
                this.checkBox1.Visible = value;
            }
        }

        [DefaultValueAttribute(false)]
        public bool ControlChecked
        {
            get
            {
                return this.checkBox1.Checked;
            }
            set
            {
                this.checkBox1.Checked = value;
            }
        }

        public BaseForm()
        {
            InitializeComponent();
        }
    }
}

In the above the default properties match up with the [DefaultValueAttribute], i.e. in InitializeComponent() checkBox1.Visible is set to true and checkBox1.Checked is false. These are the default settings for the control when dropped onto the form.

I then created the following inherited form:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : BaseForm
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Issue and steps to reproduce:

  1. When I open Form1 in the designer the properties are the in following state.

    State: ControlChecked = False - ControlVisible = True (bold)

    ControlVisible is set to True as expected, however it is bold. The [DefaultValueAttribute] is set to true in the base form, so I would have expected this property to not be in bold.

  2. I now change ControlVisible to False in the designer. The bold turns off.

    State: ControlChecked = False - ControlVisible = False

  3. I now rebuild the project and the code in Form1 gets regenerated. The ControlVisible property reverts back to True in bold.

    State: ControlChecked = False - ControlVisible = True (bold)

  4. I now change ControlChecked from False to True and it becomes bold as expected.

    State: ControlChecked = True (bold) - ControlVisible = True (bold)

  5. I rebuild the project and no change.

    State: ControlChecked = True (bold) - ControlVisible = True (bold)

  6. I now change the property of ControlVisible from True to False and rebuild the project again. ControlVisible flipped back to True in bold.

    State: ControlChecked = True (bold) - ControlVisible = True (bold)

ControlChecked appears to be working as expected. ControlVisible keeps flipping back to True when it is set to false and the bold is reversed. Seems to me that somehow the [DefaultAttributeValue] of true in the base form is not recognized.

Update: Revised to fix a bug and more exactly isolate the issue.

Update: If I set checkBox1.Visible = false in the constructor of BaseForm, then everything works as expected. So, bottom line it appears that the trouble is getting the DefaultValueAttribute of true to be recognized on the custom property in the inherited form.

like image 658
Elan Avatar asked Jul 29 '11 12:07

Elan


1 Answers

Your ControlVisible property always gets set to false:

    [DefaultValueAttribute(true)]
    public bool ControlVisible
    {
        get
        {
            return this.checkBox1.Visible;
        }
        set
        {
            this.checkBox1.Visible = false;
        }
    }

Your set method should be:

    this.checkBox1.Visible = value;
like image 169
Paul Avatar answered Sep 20 '22 03:09

Paul