0

The title is a bit vague, but my problem is this. In Microsoft visual studio im making a console application in c#, and in the code i wanted to be able to launch a windows form inside the same project. In visual basic this can be done with Application.run(formName), however if done in c# using a refrence, the windows form oppens but then immediately stops responding. What is the correct way to open a form from a console app?

class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine("Enter a number");
        int x = int.Parse(Console.ReadLine());

        if (x == 3)
        {
            menu menuInstance = new menu();    //menu would be the name of the windows form
            menuInstance.Show();



        }


        Console.ReadKey();

        }
    }
}
Omada
  • 784
  • 4
  • 11
cooldudsk
  • 65
  • 1
  • 4
  • 13

1 Answers1

1

Use the same code a WindowsForms project does, Application.Run is still the way to go:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
} 

Hard to say what you did wrong with your original attempt at that, as you didn't post that code.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • The code below is the original code that did not work. Application.run only works in Visual Basic. I'm using C# – cooldudsk Jan 05 '15 at 20:28
  • @cooldudsk I recognize that, and this code *is* C#, and is what a standard winforms project uses. If that doesn't work for you, the problem is likely somewhere else – BradleyDotNET Jan 05 '15 at 20:30
  • We'll then it must be another issue because my visual studio does not recognize Application.run, or even Application as a valid statement. – cooldudsk Jan 05 '15 at 20:33
  • @cooldudsk You may need a reference to System.Windows, and System.Windows.Forms – BradleyDotNET Jan 05 '15 at 20:34