Tips on Converting from Console to GUI

One of the most daunting tasks for any beginner programmer is building a great console application and wanting to put it into a GUI, but don’t know how. Even experts have trouble at times doing this because the design for the console application assumes output would go to one screen unless directed elsewhere. In this entry I provide a few tips on migrating that great console app over to something with a more graphical look…. on the Programming Underground!

One common question is “I made this great console application, but how do I convert it over to a GUI and build on it?” It is a great question and the process is going to be a bit different depending on several factors. One factor will be the actual programming language you are using. The second factor would be the language or platform you are converting to. For the sake of argument, I am going to say that the tips I mention below assume you are staying with the same style of language (like going from C# console to a C# windows application) and maybe changing some of the project’s general design.

Tip number 1 – Take inventory of all the parts your console application has in place already. You might feel better writing it down somewhere and making a list of all the functions and features of your console application. Does it write to file? Does it read a database? How are each of these tasks accomplished? Write down the functions that control these functions. Hopefully if the design is good they are in a self contained style. This will make the transfer easier. When you write the list, think of where each piece might go in an event driven world. Would a function be called using a button, a menu selection, or a trackbar? It is important to identify where each piece may fit in the event driven design so you know where to paste the code when it comes to moving them over. Also take note of any needed libraries you are using and find their equivalent in the new windows application platform. You may even need to write some of the functionality yourself, but lets hope not.

Tip number 2 – Create a new project, never let a program try and convert it. Yeah sure platforms like .NET offer a wizard to convert older programs over, but hardly do they do it with amazing accuracy and sometimes they complicate things. I saw it once take a 1 liner and blow it up onto like 10 hard to read ones which never worked to begin with. It is easier for you to just open up a second project from scratch, drop on the controls and move over the code piece by piece (hence the need for tip number 1).

Tip number 3 – Before moving over the code, just take a minute to layout your GUI. Put on the buttons and controls and label them properly. Get your GUI to look just the way you want before writing, pasting, or changing any code from the console application. During this process keep an eye on your list of functionality and try to put controls on that match your functions. Does your application read a file? If so, you might want to place a textbox for the user to enter the filename and a button to do the actual reading. Don’t add anything new to the GUI in the way of features or anything, just get the basics out of the console application into the new application.

Tip number 4 – Move piece by piece slowly, take your time and make sure you are doing it right. Cut and paste them right from the console app to the GUI events. Once you got your GUI all worked out, don’t just start dropping hundreds of lines of code into the new project. Figure out where each piece goes… your readfile() function would most likely be in something like a GUI button that reads “Read file”… where you printed the lines of the file to screen, you might want to tell the button event to instead output to a multiline text box. As you move each piece over, keep an eye on the console specific commands like console.writeline and quickly convert those lines of output to the right control instead. Convert each small piece you move over so that at any one time the most lines you would rewrite is 10 or less lines. The trick is to make sure that you know what controls will be taking input and which ones will be showing it. You don’t have to have all messages going to one control like you had with one screen in the console application. After you move the piece over and convert it, this is where tip number 5 comes in.

Tip number 5 – Test each piece as you go. Make sure that after you rewrite your lines that you fully test it again. There will be new errors moving from console to a windows app, so flush them out function by function if you can. Use the debugger to step through the code. This process is made easier if you made a really nice decoupled program back in your console application. Where each function has one function and does it well. It is self contained or “modularized”. Each time you add a new function, test again. The process of moving from a console or application should be done with a lot of time and loving care. Never rush this process or your GUI application will most likely cause you tremendous headaches later, causing you to spend large amounts of unneeded time.

Tip number 6 – After all the pieces are moved over, run a series of system tests. Most likely if you test each function as you go, at the end you will find the final result gives little in the way of errors. This is a good sign. Run your console application with some input and get some output. Do the same with the GUI application. Are the results coming out the same? If they don’t, you know you probably have a logic error. Step through the functions and make sure those are cleared out.

So you are done! I find going from a console to a GUI application a nice long process that can take a day to a week depending on the size of the console application and its complexity. Give this process plenty of time and be sure to do it right. It may seem slow since you are not often writing a bunch of new code, but your patience will pay off in the end with a fully functioning product.

Now remember these are just general tips and assumes your are sticking within the same family of languages. While the overall idea these tips provide could be applied to cross platform transfers, they may not always work. For instance going from a console application in C# over to Java isn’t going to be a nice clean process. The two platforms structure things very differently.

I hope this helps guide you in the process of moving from console to GUI with little difficulty. Good luck on that conversion! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 20 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.