Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I include a console in Winforms?

I would like to have a console window embedded in a Winform. Is there any way to do this?

like image 479
Arlen Beiler Avatar asked Oct 12 '10 17:10

Arlen Beiler


People also ask

How do I show a console output window in a forms application?

When debugging a Windows Forms application, the Console class is often used (in place of the Debug class) to write to the IDE's Output window. It is possible to attach one actual console window to the process by using the AllocConsole function from kernel32. dll and its matching FreeConsole to release it.

How do you console something in C#?

In C# you can write or print to console using Console. WriteLine() or Console. Write(), basically both methods are used to print output of console.

How do I add a console to Visual Studio?

Open Visual Studio, and choose Create a new project in the Start window. In the Create a new project window, select All languages, and then choose C# from the dropdown list. Choose Windows from the All platforms list, and choose Console from the All project types list.

How do I change the console application in Windows?

Right click your project in the solution explorer and select properties. Then, under the "Application" tab change the "Output type" of your project from “Console Application” to “Windows Application.” Save this answer.


1 Answers

All you need to do is call the windows API function AllocConsloe then use the normal console class here is the form code

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.Runtime.InteropServices;

namespace waTest
{
    public partial class Form1 : Form
    {
        [DllImport("Kernel32.dll")]
        static extern Boolean AllocConsole( );

        public Form1( )
        {
            InitializeComponent();
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            if ( !AllocConsole() )
                 MessageBox.Show("Failed");
            Console.WriteLine("test");
            string input = Console.ReadLine();
            MessageBox.Show(input);
        }
    }
}
like image 69
rerun Avatar answered Sep 24 '22 00:09

rerun