Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the name Application does not exist in the current context

Tags:

c#

I have an existing winform app which didn't use Program.cs. I wanted to add Program.cs with the usual code

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

but I have the error message

Application doesn't exist in the current context.

How to solve this ?

like image 472
user310291 Avatar asked Oct 31 '10 20:10

user310291


People also ask

Does not exist in the current context means?

Many a times while writing a code we get this error which says, “The name does not exists in the current context”. This is because the code behind file is not able to find the control being added to your aspx file.


2 Answers

You need to have a using directive for System.Windows.Forms in the beginning of the file:

using System.Windows.Forms;

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

Either that, or modify the Main method to use the full type names:

System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new Form1());

...though I would recommend the first solution.

like image 97
Fredrik Mörk Avatar answered Oct 21 '22 02:10

Fredrik Mörk


Make sure you added reference to System.Windows.Forms assembly and added reference to System.Windows.Forms namespace.

like image 22
Andrew Bezzub Avatar answered Oct 21 '22 02:10

Andrew Bezzub