I build a C# Windows Form Application. I have problem with the main module. (named by default of “Program.cs”)
When I try compile&run:
MessageForm f = new MessageForm("Main");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(f);
It fails, (Windows application crash message), but when:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MessageForm("Main"));
It runs great.
- Pay attention to the last command (Application.run(..)); it causes this issue.
Why won’t it run with a variable while it runs with “no variable”? (Sorry, I don’t pretty much know how to call it).
Why is that way? What is the problem?
You must call Application.SetCompatibleTextRenderingDefault before creating any windows. It will throw
InvalidOperationExceptionif called after a window is created. This is why your application is crashing.Simply move your form variable after the call if you want to write your code this way.