0

I created a new Forms project in Visual Studio and I have a richtextbox control that I made using the [Designer] that I want to be public static, but every time that I do anything, Visual Studio changes removes the "static" and undoes my changes. It only happens for elements that I created using the designer and I'm wondering if there's a way to disable it for just the richtextbox control? Or any other solutions?

Well right now all I've tried is just manually changing it back to public static every time and it works but then Visual Studio changes it back again later.

Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14
  • 1
    Does this answer your question? [How to change modifier of a control to Static in Visual Studio](https://stackoverflow.com/questions/2041557/how-to-change-modifier-of-a-control-to-static-in-visual-studio) – devlin carnate Aug 03 '23 at 20:52
  • 1
    That sounds like a terrible idea. IMHO create a separate class with static fields (or a singleton instance), then your application and control can both interact with those static fields. – Jeremy Lakeman Aug 04 '23 at 03:18
  • You have no reason to declare that Control `static`. Or trying to manually change the `designer.cs` file (you should have seen the warnings) -- You should probably describe what kind of problem you were trying to solve when you decided to go that way – Jimi Aug 04 '23 at 03:20
  • That is really a terrible idea. It will certainly cause memory leaks, and unpredictable behavior if you instantiate your form a second time. – Nick Aug 04 '23 at 07:19
  • As others advised, from the design aspect, this is not a good idea. But from the code aspect, it is feasible to set(still unable to set it in the designer file). You can take a look of my answer. :) – Bowman Zhu-MSFT Aug 04 '23 at 09:36

1 Answers1

0

How do I prevent visual studio from reverting my changes in System.Windows.Forms?

If you are taling about keep "static" definition in some designer files like Form1.Designer.cs, then it is totally impossible. The edit will be changed without mercy.

But due to it is a partial class. You can set a pubilc static variable in the original class. Like this:

enter image description here

This is not a design aspect idea, just share an idea about how to achieve this. From design aspect this is not a good choice.

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10