Reputation: 11607
Everytime I move to Designer View my whole designer.cs code is messed up :
VS Designer reorganizes code blocks and puts an irritant verbose prefixes like "this.whatever
"
and fully qualifies objects using "System.Windows.Forms.whatever
"
I Know that "Designer.cs" is not intended to be edited but I need to do some GUI code customization from time to time and would like these to stay as I changed them.
How to avoid that ? (Guess this is too funky for VS to handle)
(Actually I am just avoiding the use of the designer and do it all by hand, the good old way)
Update :
I am surprised to see the herd-like reaction towards this question. Sorry if it is disturbing, but it is interesting to see that, before hitting me on the hand saying "DON'T DO that, it's Bad", NO ONE asked WHY I wanted to do it.
IMHO the question is relevant and that's why : Many of the "auto-generated" code is rubbish and of absolutely no use, it does need some enhancement. One example (among soooo many others) : Why generate a Size/Location property when the control's Dock Mode is set to Fill ? I wanted to take advantage of the benefits whithout the drawdowns.
Anyway, I'll keep the short answer : You take it ALL (with the rubbish) or leave it ALL.
Upvotes: 3
Views: 937
Reputation: 416149
It's simple: don't edit the designer code. Those warnings are there for a reason, and for Visual Studio to work correctly it needs to own that file.
This is a partial class: everything you need to do, you can do in the matching non-designer file for that class; this includes all your control declarations and other things. Since you're trying to avoid the designer entirely, let visual studio have that file and just put everything in your normal .cs file.
Update:
Based on the comment, I want to add the following —
Either use the designer or don't use the designer. Don't be wishy-washy about it. If you're using and relying on the designer for some things, you MUST leave the designer's file alone.
If you're avoiding the designer, then really avoid the designer. Everything it does you can do in your own code (except of course for the visual queues, but even that can be done better via prototyping). You can even create your own additional file for the partial class to keep designer-like code in.
If you're only using the designer as an occasional code generator to help reduce some boring typing, do that in a separate project or on a throw-away form in your existing project and just copy/paste the code over.
Upvotes: 10
Reputation: 13360
You can always extend the designer class by putting your custom code in another cs file. The designer can't touch that and the compiler will compile your additions to that class. If you're doing something like adding properties or methods its super to simple to do:
// MyWinformExtension.cs
partial class MyWinformName
{
public string Foo { get; set; }
public void ProcessData()
{
// do some processing..
}
}
Voila! That's how you add your own code. You can't alter the designer code because it will be re-written ALL DAY LONG by the IDE. That's why there are warnings in the source and why everyone else is telling you the same thing.
Upvotes: 1
Reputation: 1504092
The designer should only be writing code in FormName.Designer.cs
which you should not touch. Visual Stuiod declares a partial class implemented in two files - one for the designer, and one for your code. I'd be surprised to hear that the designer was rewriting the non-designer file.
If this doesn't help. please give more details of which version of VS you're using, which files are being rewritten, and which bits of those files.
Upvotes: 3