37

I'm working on an older .NET code base that has all the designer code stuffed into the same code file as my code (pre - partial classes).

Is there a mechanism to tell Visual Studio 2008 to go back and refactor designer code into a X.designer.cs partial class file?

Clyde
  • 8,017
  • 11
  • 56
  • 87
  • 2
    This is duplicate of http://stackoverflow.com/questions/762528/how-to-automatically-convert-vs2003-classes-to-partial-designer-cs-files – reinierpost Sep 23 '09 at 07:47
  • I wrote myself a snipper for code of the Designer.cs, which is a good compromise between doing it all by hand, or trying to automate everything. – Benjol Feb 11 '10 at 11:02

7 Answers7

40

What I just did was completely manually:

  1. Create yourform.Designer.cs
  2. Add it to Visual Studio (right click, add existing item)
  3. Add the partial keyword to your existing class.
  4. Add the namespace exactly like in the original cs to the yourform.designer.cs
  5. Inside this namespace, add the class definition (don't forget to include the partial keyword). Do not add inheritance and/or interfaces to this partial class in Designer.cs.
  6. After this is done, you're ready to cut and paste the following:

a) Remove the components object you might have in your original Winform. If the application was .NET 1.1 you will have something like this:

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private Container components = null;

b) Add a new components object in the Designer class:

    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

c) Unless you had a specific dispose method, this is the standard. If you don't have any form Inheritance, I think that base.Dispose can be safety removed:

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose( bool disposing )
    {
        if ( disposing && ( components != null ) )
        {
            components.Dispose();
        }
        base.Dispose( disposing );
    }

d) Copy all the code inside the #region Windows Form Designer generated code to the new Designer.cs class.

e) You should also copy all the member variables for all your objects (labels, texboxes, etc. that you use in the designer).

That should be all about it. Save and compile.

Remember that a partial class can be split among N number of files, but all must share the SAME namespace.

Is it worthwhile? Well, in my case, I had a bunch of huge winforms with tons of code and controls. VS2008 crawled every time I switched from/to designer. This made the code view more responsive. I remember having to wait for 3-5 seconds before having a responsive code. Now it takes 1…


UPDATE:

Doing steps 1 to 5 and moving an existing or adding a new control won't automatically move anything to the designer.cs class. New stuff goes to the new Designer class, but old stuff remains where it was, unfortunately.

You also have to close and reopen the file (after you have added/created the partial class) for VS to draw correctly in the Designer; failure to do may result in empty forms being drawn.

JYelton
  • 35,664
  • 27
  • 132
  • 191
Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • +1 this is what I did to some of the massive forms in our project at work. Makes it so much easier to diff the changes without the forms designer code in them. – Pondidum Apr 21 '09 at 11:40
  • I'm surprised you had to go through so much trouble. Once you've created the partial class in the correct namespace, little more should have been necessary, since the Designer serializes to code (in the partial class). This is why it overwrites manual changes if you're not very careful. If you've got more of these to do, try doing all but moving the members. Then, open in Designer, change something trivial, and then Save. See if it moves the members at least. – John Saunders Apr 21 '09 at 12:39
  • That's what I thought, but if you do steps 1 to 5, you end up with an empty partial class. Moving a control in the designer then saving the form doesn't move anything to the Desginer class. (I've tried all that before resorting to manual). I'm trying again as I type. Nothing happens. Will see what is the "minimum" you have to move. – Martin Marconcini Apr 21 '09 at 15:31
7

If anyone is still looking for an automated method of doing this there's a VS macro here that will do most of it for you.

A very nice tool.

Haas
  • 428
  • 6
  • 12
  • While the link posted here works, it takes you to a blog post, that blog post has a DropBox link for the actual macro, and unfortunately that DropBox link is now dead. – David Hoffman Nov 26 '19 at 11:45
4

I have had good luck with the free stand-alone application DeCodEx (Designer Code Extractor).

DeCodeEx loads in a Visual Studio project, detects which files are WinForms, and automatically splits designer code out of the original .cs or .vb file into a new .designer.cs or .designer.vb file.

For large projects it is much easier than trying to manually split out the files yourself.

davidg
  • 5,868
  • 2
  • 33
  • 51
  • Thanks for your good suggestion. Please note that each source file must have only one class definition before convert. – Behzad Ebrahimi May 14 '20 at 13:14
  • This works, but the tool uses an old version of c# and vb.net languages, so some features like anonymous functions, & intended & _ don't work. I've use vs 2019 so I had to replace things like that before starting the conversion. The tool works with utf-8, some of my files were in other encodings, so I've to convert too. link to the utf-8 visual studio: [https://stackoverflow.com/questions/43800861/convert-all-cs-files-to-unicode-in-visualstudio] – fsbflavio Oct 28 '20 at 12:19
3

To merge the yourForm.designer.cs to yourForm.cs, Follow the below steps

Edit .csproj in notepad and add the following code

<Compile Include="yourForm.designer.cs">
    <DependentUpon>yourForm.cs</DependentUpon>
</Compile>
ebo
  • 2,717
  • 1
  • 27
  • 22
  • 1
    There is no need to do this in the latest versions of VS.NET. At least, in VS 2017. To add the .Designer.cs to the existing old form, I right-clicked the project node, selected the Add New Item command, typed in the corresponding name of the file - and VS added it as a subnode for the form's main file and created this key in the .csproj file automatically. – TecMan Oct 03 '17 at 06:24
1

Is the solution file of the older .NET code 2003/2005? I've tried to do what you're talking about by using the conversion wizard in VS2008. During conversion it should try to parse your .aspx pages and build designer files for them. What the designer generator has a problem with is that these older .aspx pages are so malformed that they simply can't be parsed.

I've come to the realization that perhaps trying to convert old ASP.NET code up to 2.0 or 3.5 is, in itself, going to be a large project. I have not yet found an effective way to factor out the designer code from old existing code, so I'd also be happy to learn of a solution here.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • I'm actually working on a winforms app. As best as I can tell, it didn't even make an attempt during the conversion to VS2008. – Clyde Mar 21 '09 at 17:09
  • Oh, my bad. Yeah, I'm not sure about your situation then; hopefully somebody can come back with something useful! – Cᴏʀʏ Mar 21 '09 at 17:16
0

If it's WinForms - I have a PowerShell script that edits the project and adds .designer.cs for all forms/controls.

You'd still have to move the designer code by hand though (or improve the script) - I only had a few forms and it wasn't worth the effort to add that too.

I can look it up and post it if that would help.

laktak
  • 57,064
  • 17
  • 134
  • 164
  • nah, since it's not fully automated it's probably not worth the trouble. As the other commenter says, it's not *that* big a deal. Just a little annoying. – Clyde Mar 21 '09 at 17:19
0

I'd be interested to see what happens if you open one of these forms in the designer and save it; or modify it and save it; or do one of the above after creating a .designer.cs file with only an empty shell of the partial class. It wouldn't surprise me to find the designer placing at least the changes into the .designer.cs file.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I've just did that. Had an old winform (NET 1.1) I opened in VS2008 with .NET35. I created (notepad) the designer.cs and added it to VS. The IDE detected it, but unless you add the 'partial' and add the class to designer.cs, VS will ignore it. Both forms must be in the same Namespace. I also had to change the System.ComponentModel.IContainer; in .NET 1.1 was different. I copied/pasted Dispose method, the definitions and the InitializeForm method. It worked. Can be a pain if the form is a mess :) – Martin Marconcini Apr 21 '09 at 11:20
  • Sorry for the typo: I've just DONE that. :) – Martin Marconcini Apr 21 '09 at 11:20