vis*_*hnu 4 .net c# console-application .net-3.5
我的项目是一个控制台客户端.我从控制台开始,然后显示表单.我使用下面的代码显示一个空白表单(我稍后会添加控件)给用户.但是表单会显示,但它会被卡住(不活动).我该怎么办?
Console.WriteLine("Starting form");
Console_Client.Main mainform = new Main();
mainform.Show();
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
试试ShowDialog().
问题是你没有运行消息循环.有两种方法可以启动一种方法.ShowDialog()有一个集成,所以将工作.另一种方法是Application.Run()在Show()调用之后或使用表单作为参数.
ShowDialog():
mainform.ShowDialog();
Run Code Online (Sandbox Code Playgroud)Application.Run() 没有形式:
mainform.Show();
Application.Run();
Run Code Online (Sandbox Code Playgroud)Application.Run() 与形式:
Application.Run(mainform);
Run Code Online (Sandbox Code Playgroud)所有这些工作.
您需要启动一个完整的应用程序,就像Windows窗体应用程序通常一样:
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();
Run Code Online (Sandbox Code Playgroud)