Custom properties defined in the base form lose their state in the inherited form when rebuilding

I am having problems with the properties of a basic form that does not support 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 for reproducing:

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 example, the default properties are the same as [DefaultValueAttribute], that is, in InitializeComponent (), checkBox1.Visible is set to true and checkBox1.Checked is false. These are the default settings for the control when it falls on the form.

Then I created the following inherited form:

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

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

Problem and steps to reproduce:

  • Form1 , .

    : ControlChecked = False - ControlVisible = True ()

    ControlVisible True, , . [DefaultValueAttribute] true , , .

  • ControlVisible False . .

    : ControlChecked = False - ControlVisible = False

  • , Form1 . ControlVisible True, .

    : ControlChecked = False - ControlVisible = True ()

  • ControlChecked False True , .

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

  • .

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

  • ControlVisible True False . ControlVisible True .

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

ControlChecked . ControlVisible True, false, - . , - [DefaultAttributeValue] .

Update: .

Update: checkBox1.Visible = false BaseForm, , . , , , DefaultValueAttribute .

+5
2

checkBox1 ? , , ( checkBox1.Visible, ControlVisible), , .

, InitializeControls Form1.designer.cs, ?

, ShouldSerialze Reset , .

Edit

VS . , ControlVisible VS, false, checkBox1.Visible ( ) false. , "" ControlVisible, , true, , , VS , . VS.

:

    public BaseForm()
    {
        InitializeComponent();
        _testValue = false;
    }

    private bool _testValue;

    [DefaultValue(true)]
    public bool TestProperty
    {
        get { return _testValue; }
        set { _testValue = value; }
    }

    protected override void OnVisibleChanged(EventArgs e)
    {            
        _testValue = true;
        base.OnVisibleChanged(e);
    }
}

BaseForm TestPropery , ControlVisible , , VS.

, bool , checkBox1.Visible :

    public BaseForm()
    {
        InitializeComponent();
        checkBox1.Visible = _controlVisible = true;
    }

    private bool _controlVisible;

    [DefaultValue(true)]
    public bool ControlVisible
    {
        get { return _controlVisible; }
        set { _controlVisible = checkBox1.Visible = value; }
    }
+2

ControlVisible false:

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

:

    this.checkBox1.Visible = value;
+4

All Articles