简体   繁体   中英

Why does visual studio remove static from my code?

I've manually set a label to a 'static', when I run the code it works fine but the next time I run the code it gives me a build error, this is because the 'static' got removed automatically. I edited this in the formMain.Designer.cs file.

I looked on google and in Visual Studio itself but didn't find anything.

Here is a link to Paste-bin as the code would take to much space: https://pastebin.com/LkPwrJhY public static System.Windows.Forms.ToolStripStatusLabel lblSerialStatus;

The label I changed is on the last line of code, 'lblSerialStatus', at line 180. I'm changing this label because I want to change it from an other form.

Thank you in advanced!

This is because formMain.Designer.cs file is auto-generated. Meaning every time you change something in the visual designer this file gets re-generated over writing any manual changes done to the file
If you really want the label somewhere else you'll need to pass it as a parameter to some method. Also, static UI elements make no sense, because the UI will not always be there or be accessible

Changing Designer.cs file is not recommended. The file is automatically generated and it will be generated again whenever you change the corresponding forms file.

look before InitializeComponent() its also mentions in your generated designer Code

#region Windows Form Designer generated code

 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>

If you want then you can create another file which contains a partial class where you can put all such logic or function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM