Running a form in a C # console project?

My project is a console client. I start in the console and then show the form. I use the code below to display an empty form (I will add controls later) to the user. But the form is displayed, but it is stuck (not active). What should I do?

Console.WriteLine("Starting form"); Console_Client.Main mainform = new Main(); mainform.Show(); Console.ReadLine(); 
+4
source share
2 answers

Try ShowDialog() .

The problem is that you are not using a message loop. There are two ways to run it. ShowDialog() has one integrated to make it work. An alternative is to use Application.Run() , either after calling Show() , or with the form as a parameter.

  • ShowDialog() :

     mainform.ShowDialog(); 
  • Application.Run() without form:

     mainform.Show(); Application.Run(); 
  • Application.Run() with the form:

     Application.Run(mainform); 

All these work.

+9
source

You need to run the full application, as the Windows Forms application usually works:

  Console.WriteLine("Starting form"); Console_Client.Main mainform = new Main(); // This will start the message loop, and show the mainform... Application.Run(mainform); // This won't occur until the form is closed, so is likely no longer required. // Console.ReadLine(); 
+5
source

All Articles