Be Esz
Be Esz

Reputation: 45

Visual Studio changes control property whenever there any changes in form

Im working on a winforms app. I have a form called "MainForm" which contains user control. There is a checkbox in the user control, that should be hidden in some scenarios - thats why I made a property for it. The problem is that whenever I open the mainform, and make any changes in it the designer changes that property to false, so the checkbox in control is not visible.

Is there any way to prevent that behavior? (I'm using VS2012)

UPDATE:

In codebehind of my control I have a propeprty

 public bool IsWebOmmitVisable
    {
        get { return ommitCheckBox.Visible; }
        set { ommitCheckBox.Visible = value; }
    }

In the constructor of control I set it to true:

 public myControl()
    {
        InitializeComponent();
        IsWebOmmitVisable = true;
...

However it looks like it doesn't matter.
Then I add this control to MainForm. The property is visible in properties of control. However whenever I modify any of the elements in MainForm, the property is set to false.

https://i.sstatic.net/0fSvQ.jpg

Upvotes: 0

Views: 167

Answers (3)

Mark Hall
Mark Hall

Reputation: 54552

Using the DesignerSerializationVisibilityAttribute will prevent the property from being serialized in the designer.

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool IsWebOmmitVisable
    {
        get { return ommitCheckBox.Visible; }
        set { ommitCheckBox.Visible = value; }
    }

From link:

With the DesignerSerializationVisibilityAttribute, you can indicate whether the value for a property is Visible, and should be persisted in initialization code, Hidden, and should not be persisted in initialization code, or consists of Content, which should have initialization code generated for each public, not hidden property of the object assigned to the property. Members that do not have a DesignerSerializationVisibilityAttribute will be treated as though they have a DesignerSerializationVisibilityAttribute with a value of Visible. The values of a property marked as Visible will be serialized, if possible, by a serializer for the type. To specify custom serialization for a particular type or property, use the DesignerSerializerAttribute.

Upvotes: 1

Hasson
Hasson

Reputation: 1914

In the properties window select the class of the mainform, then click on the events tab (lightning icon) then double click in the load event, this will add a load event in the form, here you can set visible to false or true as you wish for the checkbox control

    private void MainForm_Load(object sender, EventArgs e)
    {
        mycheckbox.Visible = false;
    }

Upvotes: 0

mfc
mfc

Reputation: 11

simple form image

and code inside the Form1.cs

private void Form1_Load(object sender, EventArgs e)
{
    textBox1.Visible = false;
}

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Visible = !textBox1.Visible;
}

And it works perfectly as it should. (Button toggles the Visible property of the textbox.) I am not sure at what step you went wrong.

The Form1_Load was autogenerated by double clicking form title in the designer.

The button1_Click was autogenerated by double clicking the button1 in the designer.

Upvotes: 0

Related Questions