Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows form from console

I would like to spawn a Windows form from the console using C#. Roughly like display does in Linux, and modify its contents, etc. Is that possible?

like image 298
Dervin Thunk Avatar asked Oct 26 '09 20:10

Dervin Thunk


People also ask

Can I add a form to Console Application C#?

You need to create a Windows application. Then right click the project and go to properties and set the Output Type for the application to Console Application. This will then not only show your form but also the Console Window. So now you can use the Console class to read in or display messages to that console Window.

Can Windows Forms run on Linux?

Windows Forms (and WPF) are both not supported on Linux with . NET Core and probably never will be. Winforms runs on mono, but that is based on . NET Framework, not .

Where can I find system Windows Forms?

Forms. Integration namespace is defined in 'WindowsFormsIntegration. dll' which currently ships in the WinFX SDK, not in the standard redist. Therefore, the file will be found in '\Program Files\Reference Assemblies\Microsoft\Framework\v3.


2 Answers

You should be able to add a reference for System.Windows.Forms and then be good to go. You may also have to apply the STAThreadAttribute to the entry point of your application.

using System.Windows.Forms;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        MessageBox.Show("hello");
    }
}

... more complex ...

using System.Windows.Forms;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var frm = new Form();
        frm.Name = "Hello";
        var lb = new Label();
        lb.Text = "Hello World!!!";
        frm.Controls.Add(lb);
        frm.ShowDialog();
    }
}
like image 102
Matthew Whited Avatar answered Oct 23 '22 02:10

Matthew Whited


Yes, you can initialize a form in the Console. Add a reference to System.Windows.Forms and use the following sample code:

System.Windows.Forms.Form f = new System.Windows.Forms.Form(); 
f.ShowDialog(); 
like image 29
Yuriy Faktorovich Avatar answered Oct 23 '22 03:10

Yuriy Faktorovich