Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a form in C# console project?

My project is a console client. I start in the console and then display the form. I use the code below to display a blank 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();
like image 484
vishnu Avatar asked Dec 09 '22 13:12

vishnu


1 Answers

Try ShowDialog().

The problem is that you're not running a message loop. There are two ways to start one. ShowDialog() has one integrated so that will work. The alternative is to use Application.Run(), either after the Show() call or with the form as a parameter.

  1. ShowDialog():

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

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

    Application.Run(mainform);
    

All of these work.

like image 80
Pieter van Ginkel Avatar answered Dec 14 '22 23:12

Pieter van Ginkel