views:

33

answers:

1

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.

A: 

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.

Hans Passant
Righto. Can't modify InitializeComponent like you suggest since that gets regenerated with each form modification, but the constructor works too. I was hoping to avoid having to do the assignment manually to each form but it's not the end of the world.
nealyoung