简体   繁体   English

只要表单发生任何更改,Visual Studio就会更改控件属性

[英]Visual Studio changes control property whenever there any changes in form

Im working on a winforms app. 我正在开发winforms应用。 I have a form called "MainForm" which contains user control. 我有一个名为“ MainForm”的表单,其中包含用户控件。 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. 问题是,每当我打开主窗体并对其进行任何更改时,设计器都会将该属性更改为false,因此控件中的复选框不可见。

Is there any way to prevent that behavior? 有什么办法可以防止这种行为? (I'm using VS2012) (我正在使用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: 在控件的构造函数中,将其设置为true:

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

However it looks like it doesn't matter. 但是看起来没关系。
Then I add this control to MainForm. 然后,我将此控件添加到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. 但是,每当我修改MainForm中的任何元素时,该属性就会设置为false。

http://i.stack.imgur.com/0fSvQ.jpg http://i.stack.imgur.com/0fSvQ.jpg

Using the DesignerSerializationVisibilityAttribute will prevent the property from being serialized in the designer. 使用DesignerSerializationVisibilityAttribute将防止在设计器中序列化属性。

   [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. 使用DesignerSerializationVisibilityAttribute,您可以指示属性的值是否为Visible,是否应保留在初始化代码(隐藏)中,而不应保留在初始化代码中,或由Content组成,Content应该为每个公共对象生成初始化代码,没有隐藏属性的对象分配给该属性。 Members that do not have a DesignerSerializationVisibilityAttribute will be treated as though they have a DesignerSerializationVisibilityAttribute with a value of Visible. 没有DesignerSerializationVisibilityAttribute的成员将被视为具有具有Visible值的DesignerSerializationVisibilityAttribute。 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. 要为特定类型或属性指定自定义序列化,请使用DesignerSerializerAttribute。

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 在属性窗口中,选择主窗体的类,然后单击事件选项卡(闪电图标),然后双击加载事件,这将在表单中添加一个加载事件,在此处您可以将visible设置为false或true希望复选框控件

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

simple form image 简单表格图片

and code inside the Form1.cs 和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. (按钮可切换文本框的Visible属性。)我不确定在哪一步出错。

The Form1_Load was autogenerated by double clicking form title in the designer. 通过双击设计器中的表单标题可以自动生成Form1_Load

The button1_Click was autogenerated by double clicking the button1 in the designer. 通过双击设计器中的button1,可以自动生成button1_Click

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测窗体的任何控件的更改? - How to detect changes in any control of the form? 如何在VisualTreeHelper.GetParent()更改时将属性附加到控件或使用会更新的绑定? - How can I attach a property to a control or use a binding that updates whenever VisualTreeHelper.GetParent() changes? 属性更改时如何更改任何控件的外观? - How to change the appearance of any control when property changes? Visual Studio 2010 - 中断任何更改对象属性的内容 - Visual Studio 2010 - Break on anything that changes an object property Visual Studio每次运行都会构建Xaml文件(不做任何更改) - Visual Studio Builds Xaml Files Every Run (Without Any Changes) Visual Studio:Git 团队资源管理器未显示任何更改 - Visual Studio: Git Team Explorer does not show any changes 由于MinimumSize重新打开Visual Studio窗体设计器更改布局 - Visual Studio Form Designer Changes Layout When Reopened Because Of MinimumSize 为什么 Visual Studio 会自动更改表单的布局? - Why does Visual Studio automatically changes the layout of my form? 每当对表单内的文本框,组合框等进行任何更改时都会触发事件 - Have event fire whenever any changes made to textboxes, comboboxs, etc. inside form Visual Studio跳过代码更改 - Visual studio skip changes in code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM