2

I am making a C# windows form application. I want Console.write() in my form application. I read lot of stuff. But did not work. Can you give me a clean code to achieve this ? Thanks in advance.

P.S.

[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;

public Form1()
{
    InitializeComponent();

    AttachConsole(ATTACH_PARENT_PROCESS);
    Console.WriteLine("This is from the main program");
}

This is not working for me.

default locale
  • 13,035
  • 13
  • 56
  • 62
user3218743
  • 579
  • 2
  • 8
  • 28

3 Answers3

3

Bringing up the console on Form Application is difficult If you're looking for a way to show an output, you can use

MessageBox.Show("TEXT HERE");

And that will bring up a dialog box with the text you used.

Hopefully that helps you out :)

Tim
  • 370
  • 5
  • 9
3

I think you're complicating things. You can get what you want just by changing project properties.

  • Create windows application project, show new form etc
  • Go to project properties->Application-> Change output type-> Console Application.

and you're done. you get a console for free in windows application.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Thanks. But I need Windows form capabilities too. Like buttons and text-boxes. Console to show the dataflow. – user3218743 May 18 '14 at 06:17
  • @user3218743 Did you try this? You can get all what you need. You can just convert your existing winforms application to console app type. That's it. Form will be shown as usual with a console window also, – Sriram Sakthivel May 18 '14 at 06:19
1

You might enjoy using System.Diagnostics for its Debug.WriteLine() which works great in windows forms apps and will not accidentally spill over into Release configurations.

According to your edit, and @elgonzo's suggestion, you've now prepared the app to attach to a console but ... are you running the app from cmd.exe? If you do, the calls to Console.Write should end up there.

clarkitect
  • 1,720
  • 14
  • 23