As I promised in yesterday’s post, I’m now going to show you an even easier way to convert a C# console application into a regular Windows application. Since the steps are nearly identical, I’m just going to focus on the key points instead of writing the same thing as before.
First, perform the following steps:
- Create a regular C# console application or open one of your own.
- Add the following two references to your project and the corresponding “using” statements on top of the main source file:
using System.Drawing
using System.Windows.Forms
- Remove the “args” string array parameter from the Main() function.
- Add “Application.EnableVisualStyles();” as the first call in the Main() function.
- Change the “Output type” of your project from “Console Application” to “Windows Application.”
Now for the fun part (the part that’s different from my last post). Do the following:
- Change the “Program” class to derive from “System.Windows.Forms.Form.”
- Add “Application.Run(new Program());” as the last call in the Main() function.
At this point your “Program” class should look like this:
class Program:System.Windows.Forms.Form
{
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Program());
}
}
We’re almost done. In fact, you can run the program now and you’ll see a blank form. But you won’t be able to place any controls on it. Well, actually that’s not true – you can place controls, but the form will still be blank at runtime. The last step is:
- Add a constructor to the “Program” class and call “InitializeComponent()” inside it:
class Program:System.Windows.Forms.Form
{
Program() // ADD THIS CONSTRUCTOR
{
InitializeComponent();
}
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Program());
}
}
“InitializeComponent()” will not be defined yet. However, the Form Designer will create this function as soon as any control is placed on the form or its layout is modified. You may have noticed that the icon next to “Program.cs” (in Solution Explorer) has changed to a form instead of the one representing a code file:
This means you can double-click on it and Visual Studio will open the Form Designer instead of going to the source. Try this:
- Double click on Program.cs.
- Change the form’s size in the Designer.
- Go back to the source window of Program.cs.
Now you should see the “InitializeComponent()” function defined underneath “Main().” Your “Program” class will now look like this:
class Program:System.Windows.Forms.Form
{
Program()
{
InitializeComponent();
}
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Program());
}
private void InitializeComponent() // DESIGNER WILL ADD THIS FUNCTION
{
this.SuspendLayout();
//
// Program
//
this.ClientSize = new System.Drawing.Size(367, 188);
this.Name = “Program”;
this.ResumeLayout(false);
}
}
You’re done! 🙂 Everything is contained in this one source file. The designer will keep on modifying “InitializeComponent()” whenever something changes in the form itself. It will also add the necessary member variables to the “Program” class whenever controls are placed on the form.
-Greg Dolley
*Get new posts automatically! Grab the RSS feed here. Want email updates instead? Click here.