I would like to have a console window embedded in a Winform. Is there any way to do this?
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.
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.
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.
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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With