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 ?
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.
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.
Make sure you added reference to System.Windows.Forms
assembly and added reference to System.Windows.Forms
namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With