本文介绍了C#控制台项目 - >添加Windows窗体的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:)



出于测试目的,我创建了一个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

推荐答案

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!


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


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窗体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:14