Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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
Posted
Updated 29-May-23 23:30pm

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:

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

    }
}


And In the Form :

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


I hope this will help!
 
Share this answer
 
find main method wich is first method to start debug !
write this line :
C#
System.Windows.Forms.Application.Run(new Form1()); // i consider Form1 is your windows form class

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!
C#
[STAThread]
 static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }
 
Share this answer
 
In your Program.cs
You Must Run the GUI on another Thread :

Just add For Exemple:

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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900