.NET / Microsoft

Output To Console In Windows Forms Application

This post shows you how to output data to a Console in a Windows Forms Application. I use this technique religiously when developing Windows Forms applications. Debugging is much simpler when you can see information on what your application is doing in a Console. Of course the onus is on you as developer to output data which will help you.

In this post I will use a simple Windows Forms application which adds two numbers.

Windows Forms

By default a Windows Forms application does not output to a Console even if you write a Console.Write() or Console.WriteLine(). This shortcoming can be addressed by using Win32 API. In your Windows Forms application you can declare a class which provides a wrapper around Win32 functions.

A Console must be started to accept input and display our messages. This can be done by calling Win32.AllocConsole() function. For my example I will start the console in my form’s constructor.

Now when I write to Console, I will see data appear in the console window which was started in my constructor.

Console

As a good practice you should also close the console window by calling Win32.FreeConsole() method before your application exits.

Loading Facebook Comments ...

16 Comments

  1. Thanks for the tip. Too bad Console.Clear does not work under Vista although works fine under XP.

  2. Thanks, just what I needed

  3. Thanks Ed. I’m glad that you found this article helpful.

  4. It is great. Very helpful
    Thank you.

  5. It worked.
    Thanks !

  6. Thank you for publishing this solution. I have tried several different offered solution and yours is the only one the worked the very first time. Great job!

  7. Bob, Thanks for your motivating comment.

  8. This worked for me after I added
    using System.Runtime.InteropServices;
    Thanks for the how-to.

  9. You, sir, deserve a medal.

    I’ve been to 5 different sites where people invent the most cumbersome and enormous code to accomplish what you did in a simple and elegant manner.

    Thank you.

    Also thanks to pgn674 for the useful comment.

  10. OMFG, thanks and I gladly accept the medal :)

  11. Thanks Deepak, good work

  12. Guys can someone transfer this to c++ ?

  13. When closing the allocated console it causes the termination of the main application.
    Is there a way to prevent it?

  14. Thanks~very useful~~

  15. Konstantin says:

    Big Thanks To the Author: It’s a very comprehensive and useful article (that probably saved me a lot of time)

  16. Thank you Konstantin an Wei for your comments. I am glad that this post was useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">