C#控制台项目 - >添加Windows窗体的问题 [英] C# Console Project --> Problem adding Windows Forms
问题描述
你好:)
出于测试目的,我创建了一个C#ConsoleLine应用程序。现在我想做一个GUI,所以我选择Project - >添加组件并添加Windows窗体元素。但是,虽然没有Console.WriteLine或其他东西,他总是打开控制台,他不会显示我的Windows窗体元素。我怎么办,控制台消失,Windows窗体元素出现?
非常感谢你帮我解决我的新手问题:)
祝你好运,
Florian
Hello :)
For testing purposes I created a C# ConsoleLine application. Now I want to do a GUI and so I choose Project --> Add component and added a "Windows Forms" element. But although there is no Console.WriteLine or something left he always opens the Console and he does not show my Windows Forms Element. What am I to do that the Console disappears and the Windows Forms Element appears?
Thank you a lot for helping me in my newbie question :)
Best regards,
Florian
推荐答案
通过保持同一个项目,您可以更改输出类型:
1.转到项目的属性(右键单击VS中的项目)。
2.在上下文菜单中,选择属性。
3.在出现的窗口中,选择应用程序 tab(默认情况下应该是第一个)。
4.更改 Windows应用程序的输出类型。保存更改。
5.在Program.cs文件的Main函数中输入此代码:
By keeping the same project, you can change the output type:
1. Go to the properties of your project (right click on the project in VS).
2. In the context menu, choose properties.
3. In the window that appears, Select the Application tab (Should be the first one by default).
4. Change the Output Type for Windows Application. Save the changes.
5. In the Main function of the file Program.cs put this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms; // This import is needed.
namespace MyTest
{
class Program
{
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main()); // Main here is the name of my form.
// Put the name of YOUR form or Constructor
// if it is not the same.
}
}
}
并在表格中:
And In the Form :
// This is the constructor of your form.
public Main()
{
InitializeComponent();
Load += new EventHandler(Main_Load); // Optional. Just an on Load event.
}
// The is the event on Form load. it is optional.
private void Main_Load(object sender, EventArgs e)
{
listener.start();
}
我希望这会有所帮助!
I hope this will help!
find主要方法是第一种开始调试的方法!
写下这一行:
find main method wich is first method to start debug !
write this line :
System.Windows.Forms.Application.Run(new Form1()); // i consider Form1 is your windows form class
这一行会使你的窗体形成并显示出来!
别忘了将System.Windows.Forms添加到你的项目参考中!
这是VisualStudio将为Windows窗体应用程序制作的典型Main方法!
this line will make your windows form and will show it !
do not forget to add System.Windows.Forms to your project references!
and this is a typically Main method wich VisualStudio will make for a windows form application!
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
在你的Program.cs中
你必须在另一个线程上运行GUI:
只需添加For例:
In your Program.cs
You Must Run the GUI on another Thread :
Just add For Exemple:
Console.Write("Running the GUI...\nPress Enter To Abort");
new System.Threading.Thread(()=>{System.Windows.Forms.Application.Run(new Form1());}).Start();
Console.ReadLine();
System.Windows.Forms.Application.Exit();
这篇关于C#控制台项目 - >添加Windows窗体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!