nealyoung
nealyoung

Reputation: 88

Converting old VS 2002 Windows Forms projects to newer versions

I have an application that was started in C# 1.0 using Visual Studio .NET. As part of a partial rewrite/upgrade, I would like the application to take advantage of the form auto-scaling features in Windows Forms 2.0. Those features rely on setting the AutoScaleDimensions property, which is supposed to be done by the designer. New custom Forms that I add to the project are set up with the correct designer code, but all my existing forms and controls do not auto-generate this property, and thus do not get auto-scaled correctly. I believe this is because the old forms were created with the old VS2002 project format, where all designer-generated code gets dumped into the same file as user created code; modern WinForms applications, of course, get split into multiple files by VS.

My question: is there any known way to "upgrade" an old Windows Forms project to the modern Visual Studio formats? (The project files have been upgraded to VS2010 formats, but the forms themselves still utilize the mono-file format.) I would like VS to generate the AutoScaleDimensions property by default and not have to initialize the property by hand.

Upvotes: 2

Views: 528

Answers (2)

Stu
Stu

Reputation: 15769

You can do this, but you have to do it manually:

  • Copy the entire .cs file to .designer.cs
  • Add the partial keyword to the .designer one
  • In the .designer one, remove everything except the Windows Designer Generated Code
  • Do the opposite in the .cs (remove the Windows Designer Generated Code)
  • Add the .designer.cs to the project

Fairly straightforward, but very labor-intensive to do for more than a few forms.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941377

You are not going to get help converting the form class to a partial class. There really isn't any need to, the designer still supports the old style. If editing the form layout doesn't automatically insert the property assignment then just paste it in yourself, just before the ClientSize assignment. Try it out on a sample project first, just drop a button on the form and look at the Designer.cs file content. It ought to resemble:

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(...)
        // etc..

You can try it out to see if the form scales properly without having to reboot your machine by pasting this in your form's code:

    protected override void OnLoad(EventArgs e) {
        this.Font = new Font(this.Font.FontFamily, 125f / 96 * this.Font.SizeInPoints);
        base.OnLoad(e);
    }

Tinkering with layout properties might be necessary to make it look good.

Upvotes: 1

Related Questions