Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace csharptesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Hello world");
            Console.ReadLine();            
            //textBox1.Text = "Hello world";
        }
    }
}

Can't see any output for this code.
Is it possible in Windows Application?
Posted
Updated 20-Jul-11 20:15pm
v2

Here is the simplest way:

Create a regular Windows application, WPF or Forms. Go to Project properties and change application type to "Console Application". You application will become a console application and windows application at the same time.

Windows application and console application are not mutually exclusive. "Console application" really means "Show console". It does not mean "Don't create windows application". "Windows application" simply means "don't show console". By the way, if you use Console methods in non-console application, they will work (in a way)! Only without showing the results. But, for example, Console.Beep will always work.

—SA
 
Share this answer
 
v2
Comments
Mohammad A Rahman 21-Jul-11 3:20am    
Good answer. My 5! :)
Sergey Alexandrovich Kryukov 21-Jul-11 3:21am    
Thank you, Mohammad.
--SA
See this[^]. This will help you actually display the console in a forms application.
 
Share this answer
 
Comments
Tarun.K.S 21-Jul-11 3:50am    
Good article. Learned something new today. Thanks.
5+
Abhinav S 21-Jul-11 3:56am    
So did I actually. :)

Thanks for the 5.
DontSailBackwards 3-Mar-15 17:37pm    
Dead link, very sad. Yay internet archive: http://web.archive.org/web/20100815055904/http://www.thereforesystems.com/output-to-console-in-windows-forms-application
It is not impossible but why would you like to do so? Anyway to answer your question, I created a simple Windows App using C#, I just copied the code below for you,

C#
using System.Windows.Forms;

namespace GUI_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            AllocConsole();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Console.WriteLine("Hello World");
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool AllocConsole();
    }
}


Please ignore the naming convention, it is just a test app.

Hope it helps :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jul-11 3:17am    
There can be some reasons, for example, during development, to be removed later.
Now, what you're doing is too complex. The answer is really, really simple.
Please see my solution.
--SA
Mohammad A Rahman 21-Jul-11 3:19am    
I agree with you. :)
DontSailBackwards 3-Mar-15 17:51pm    
This, and the solution by Abhinav S are good when you want to build a DLL, not an EXE.
Console.WriteLine is work only in console application. so not display any output
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jul-11 3:09am    
Not quite correct. What's "console application" It's quite easy to show both console and windows application. This method does work.
Please see my answer.
--SA
johannesnestler 21-Jul-11 10:19am    
Vote of 1: wrong

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900