How to Convert a Console App into a Windows App in C# – Part Two
Posted by gregd1024 on February 9, 2008
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.
Aqil Ahmed said
Nice blog
sakina said
this thng is not workin for convertin a consle app into windows i hv a code written in console app n hv to convert it in win app n this method is not workin can u plz tell me how to convert it is it by convertin string?
gregd1024 said
Sakina, I don’t understand your question. What part isn’t working?
-Greg
steve said
In console app, the System doesn’t have
using System.Drawing;
using System.Windows.Forms;
-steve
gregd1024 said
Steve,
Ah, I guess this part wasn’t clear. The previous tutorial shows that you have to add the dll references first, and then you can type in the “using” statements. So just click “Add Reference” when right-clicking on the references folder in Solution Explorer. Pick those two dll’s out of the list (“System.Drawing” and “System.Windows.Forms”).
-Greg
Anshuman said
I have a console application project written in C. I have to convert it to a windows application (for windows mobile). Can you help?
gregd1024 said
Anshuman,
I’d suggest downloading the Windows Mobile 6 Development SDK. That will give you a wizard where you can create a bare-bones C++ windows mobile app. From there you can figure out how to convert your C project.
-Greg
Abdullah said
Man, I really do not know how to thank you. You can’t imagine how much time you saved me.
Tom said
But what do I convert Console.WriteLine() to use?
gregd1024 said
Tom – Console.WriteLine() becomes ineffective in a WinForms app unless your program instantiates a console window along with the regular program form. I don’t think a lot of programmers are aware that you can have both at the same time. Having a console window in the background is helpful for printing debug messages, and is where Console.WriteLine() would write its output. But if you don’t have a console window in your WinForm app, then just delete the Console.WriteLine() lines of code – they become useless.
-Greg Dolley
Tom said
Ultimately, what I want is a Windows version of a console program with a window that can be resized and a buffered page for writing to and reading from. Of course, it would be better if cmd.exe would allow widening that would be helpful, too.
Tom said
Doh! Properties->Layout. etc. You get the idea.
gregd1024 said
Tom – Yeah, I knew what you meant. 😉
gregd1024 said
Tom – why not just make a form with a multi-line text box that occupies the entire window?
X said
Would you just change all the Console.Writeline to a MessageBox.Show?
gregd1024 said
X – basically, yes.
Ryan said
this is great, will it allow code created for form objects to access variables and functions in my original app?
Also, in cmd you can make the window wider. Right click the title bar, properties, layout, width.
gregd1024 said
Ryan, yes – you can access variables/functions in your original app through the “Program” static object or whatever you’ve named it. To access non static stuff, I’d pass in an instance of the Program object into the main Form constructor and save that instance in some class variable.
-Greg Dolley
Johann Pascher said
For all who want to keep the original ConsloeWrite lines:
(
public class Win32
{
///
/// Allocates a new console for current process.
///
[System.Runtime.InteropServices.DllImport(“kernel32.dll”)]
public static extern Boolean AllocConsole();
///
/// Frees the console.
///
[System.Runtime.InteropServices.DllImport(“kernel32.dll”)]
public static extern Boolean FreeConsole();
}
http://dotnetperls.com/textbox-appendtext
Commandline Arguments:
http://www.howtogeek.com/howto/programming/get-command-line-arguments-in-a-windows-forms-application/
Johann Pascher said
Cant edit but here is the link:
http://www.thereforesystems.com/output-to-console-in-windows-forms-application/
Shiney said
Really Great Article :)-
ravi k dake said
hi,
can any body tell how to convert windows 32 application to console application.
please email to dakesun@gmail.com