Windows form from console

I would like to create a windows form from the console using C #. About how displayit does on Linux and modifies its contents, etc. Is it possible?

+5
source share
4 answers

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

using System.Windows.Forms;

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

... more difficult...

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();
    }
}
+6
source

Yes, you can initialize the form in the console. Add a link to System.Windows.Forms and use the following code example:

System.Windows.Forms.Form f = new System.Windows.Forms.Form(); 
f.ShowDialog(); 
+4
source

:

[STAThread]
static void Main()
{    
   Application.Run(new MyForm());
}

( ), , , ,

Thread t = new Thread(new ThreadStart(StartNewStaThread)); 

// Make sure to set the apartment state BEFORE starting the thread. 
t.ApartmentState = ApartmentState.STA; 
t.Start(); 

private void StartNewStaThread() { 
    Application.Run(new Form1()); 
} 

.

Thread t = new Thread(new ThreadStart(StartNewStaThread)); 
t.Start();

[STAThread]
private void StartNewStaThread() { 
    Application.Run(new Form1()); 
} 
+4
source

You can try this

using System.Windows.Forms;

[STAThread]
static void Main() 
{
    Application.EnableVisualStyles();
    Application.Run(new MyForm()); 
}

Bye

+1
source

All Articles