If you need to code a C# program that has both a window interface and a console, follow these steps:
- Create a new Windows Application project (called FormConsole, for example)
- In the properties of the project, set Output Type as Console Application
- Open the Program.cs file and paste the following code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace FormConsole
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 tf = new Form1();
Thread ct = new Thread(
new ThreadStart(
delegate()
{
while (true)
{
string command = Console.ReadLine();
switch (command)
{
case "close":
tf.Invoke(new MethodInvoker(
delegate()
{
tf.Close();
}));
break;
case "max":
tf.Invoke(new MethodInvoker(
delegate()
{
tf.WindowState = FormWindowState.Maximized;
}));
break;
case "min":
tf.Invoke(new MethodInvoker(
delegate()
{
tf.WindowState = FormWindowState.Minimized;
}));
break;
case "res":
tf.Invoke(new MethodInvoker(
delegate()
{
tf.WindowState = FormWindowState.Normal;
}));
break;
}
}
}));
ct.Start();
tf.Show();
tf.Activate();
Application.Run(tf);
Environment.Exit(0);
}
}
}
- Save, Build and Run.
This program will open a console and a form. The console accepts commands that can control the behavior of the form. The commands are:
- min: minimizes the window.
- max: maximizes the window.
- res: restores the window.
- close: closes the window and exits.
The general idea is to have two threads in the program: one thread to manage the Form and one to manage the Console. I kept the the main thread as the one managing the graphical interface because I’m afraid that the attribute [STAThread] initializes the thread in order to interact with COM, for example during drag&drop events.
The Console interacts with the Form using the Invoke function because the Form runs in a different thread. I’m using anonymous functions for brevity. If you use C# 1.0 then you should replace them with delegate instances. The Form.Show and Form.Activate members are called to bring the form on top of the console. The Environment.Exit function is called to exit the program even if the console thread is still alive.
::
::
::
::
::
::
::
::
::
::
::
::
::
::
::
:: 
Entries
Nathan Fiscaletti
2011/11/18
Um, I am verry new to VB, I tried this out and after i pasted the code i got these two errors…
Error 1 The type or namespace name ‘Form1′ could not be found (are you missing a using directive or an assembly reference?) C:\Users\Brett\AppData\Local\Temporary Projects\WindowsFormsApplication1\Program.cs 15 13 WindowsFormsApplication1
Error 2 The type or namespace name ‘Form1′ could not be found (are you missing a using directive or an assembly reference?) C:\Users\Brett\AppData\Local\Temporary Projects\WindowsFormsApplication1\Program.cs 15 28 WindowsFormsApplication1
Help?
Balau
2011/11/19
In Visual C# Express 2005 (which is the development environment I used) when you choose to create a new “Windows Application” project it automatically creates an empty form, called Form1 (it is the name of the class), which you can then modify by adding functionality.
In your case I suspect you don’t have this automatically created Form1 maybe because in newer version of Visual Studio they changed the name.
You can replace Form1 with the form you want to show.
Nathan Fiscaletti
2011/11/23
I have already tried that…
Balau
2011/11/23
Are the console (Program) class and the form (Form1) class in the same namespace? If they are not, you must add “using …” with the namespace of the form on top of the Program file.