Create a new console project and add a reference to System.Windows.Forms
. Add a new form to your project. As an example, I added one button to the form and set it's DialogResult
to Ok
.
In your program's main method, create an instance of your form and open it using Show
or ShowDialog
. Here's an example:
static void Main(string[] args)
{
Console.WriteLine("Opening window...");
var result = new TestForm().ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
Console.WriteLine("Form closed by button.");
else
Console.WriteLine("Form closed otherwise.");
Console.ReadLine();
}
From your form any location within your program you can use the static Console
class to access the console. Here's an example that prints some status info from the form's constructor:
public partial class TestForm : Form
{
public TestForm()
{
InitializeComponent();
Console.WriteLine("Form initialized.");
}
}
If you already have created your project as class library or windows forms project, right-click it, navigate to properties and switch the "Output type" to "Console application".