当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Prevent Visual Studio from updating specific lines in Form.Designer.cs

问题描述:

I was wondering is it possible to prevent Visual Studio from updating specific lines that are changed by me?

For example i have separate resource only project (images, sounds, etc). I change some lines in Form.Designer.cs and make so all images are loaded from resource dll. But once i update Form it self everything goes back to default and all resources that were used by form gets copied to Form.resx file.

How could i solve this?

网友答案:

It is preferable to separate the code that the form designer generates from the code that you want to have some control over. The order in which you need to address this code can then be handled within the constructor of the form. Example:

namespace FormTest
{
    public partial class Form1 : Form
    {
        private Label PostAddedLabel;

        public Form1()
        {
            InitializeComponent();
            PostInitializeComponents();
        }

        private void PostInitializeComponents()
        {
            if (!this.DesignMode)
            {
                PostAddedLabel = new Label();
                PostAddedLabel.Left = 100;
                PostAddedLabel.Top = 30;
                PostAddedLabel.Text = "The Post-added Label";

                this.Controls.Add(PostAddedLabel);
            }
        }
    }
}

We can simply design the form within the designer, after a successful design phase we then can MOVE the declaration, assignments and related code that we want to separate to the PostInitializeComponents method.

By using the !this.DesignMode decision, the form will show the separated controls in Runtime mode. When in designer-mode these controls will not be shown, assuring that the system will not affect these controls when designing the form.

In case you want to use this methodology also in usercontrols, try to embed the "IsDesignerHosted" method over "DesignMode" from the following article: DesignMode with Controls

Hope this answers the question?

网友答案:

Nope.

As stated in the begining of the file, the *.Designer.* is an auto generated file. It's rebuilt every time that the file it depends upon is saved, so you should never change any code there that you don't want to be messed.

网友答案:

No. Visual Studio does not "update" the Designer file, it deletes it and writes an all new copy. No possible way to do what you want.

You should add your code to your code behind. It's the same class.

分享给朋友:
您可能感兴趣的文章:
随机阅读: