【解決方法】C# コンソール プロジェクト –> Windows フォームの追加の問題

プログラミングQA


こんにちは :)

テスト目的で、C# ConsoleLine アプリケーションを作成しました。 ここで GUI を実行したいので、[プロジェクト]–>[コンポーネントの追加]を選択し、「Windows フォーム」要素を追加します。 しかし、Console.WriteLine などが残っていないにもかかわらず、彼は常にコンソールを開いて、私の Windows フォーム要素を表示しません。 コンソールが消えて Windows フォーム要素が表示されるにはどうすればよいですか?

私の初心者の質問を手伝ってくれてありがとう:)

よろしく、
フロリアン

解決策 4

同じプロジェクトを維持することで、出力タイプを変更できます。

1. に移動します。 プロパティ あなたの 計画 (VS でプロジェクトを右クリックします)。
2. コンテキスト メニューで、 プロパティ
3. 表示されるウィンドウで、 応用 タブ (デフォルトでは最初のタブです)。
4. を変更します。 出力タイプ ために Windowsアプリケーション。 変更を保存します。
5. Program.cs ファイルの Main 関数に次のコードを入力します。

C#
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.
            
        }

    }
}

そしてフォームでは:

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

これがお役に立てば幸いです!

解決策 2

デバッグを開始する最初のメソッドである main メソッドを見つけます。
次の行を書きます:

C#
System.Windows.Forms.Application.Run(new Form1()); // i consider Form1 is your windows form class

この行はウィンドウを作成し、それを表示します。
System.Windows.Forms をプロジェクト参照に追加することを忘れないでください。
これは、VisualStudio が Windows フォーム アプリケーション用に作成する典型的な Main メソッドです。

C#
[STAThread]
 static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }

解決策 3

Program.cs 内
GUI は別のスレッドで実行する必要があります。

例を追加するだけです:

C#
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();

コメント

タイトルとURLをコピーしました