-4

So I'm mostly new to C#, but I have created Forms before with it. I've searched for over an hour to try to find an answer, but everything I find either is hard to understand or worded in a way I don't understand, or it doesn't appear relevant.

The most relevant results appear to be these:

ObjectDisposedException when .Show()'ing a form that shouldn't be disposed

Exception when closing Form (thread + invoke)

I still don't understand though.

What I am doing is this:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 Form = new Form1();
Application.Run(Form);
Form.Show(); // Visual Studio highlights this line.
Form.UserData.Rows.Add(new string[] { "TestName", "1337" }); // I'm trying to get used to DataGridViews.
Form.UserData.Rows.Add(new string[] { "AnotherName", "9015" });

I don't see what I'm doing that requires it when close the only Form in my project.

Community
  • 1
  • 1
  • 1
    Application.Run doesn't return until you close the Form passed to it, So when you call Show the variable Form is no more valid. By the way your choice to name a variable Form is highly confusing – Steve May 18 '15 at 17:32
  • Oh, thank you very much! Sorry about the variable names, I am obsessed with capitalizing the first letter of every word (No underscores), and I am by no means creative with variable naming. – Jarod Whiting May 18 '15 at 17:40
  • Just an advice, you can choose whatever name or naming convention you like, but avoid to name variables with the same name of framework classes. – Steve May 18 '15 at 17:46
  • I'm so unoriginal and non-creative that I often put an underscore in front of variables because I have an existing variable under the same name. My OCD reaches a contradiction because I can not name it something creative, I don't want to name it something I already have, and I can't name it something that doesn't match my style. I'll work on it though. – Jarod Whiting May 18 '15 at 18:09

1 Answers1

0

Am I the only one that noted that the name of the variable is Form which is also the name of the Class Form from which Form1 is surely derived?

Try to remember to use lowercase to name the local variables and probably all things will go better.

Form1 frm = new Form1();

Application.Run(frm);

The above code should do, if you run a form The Show is done by default.

Sabrina_cs
  • 421
  • 3
  • 18
  • 1
    While I agree with you about the naming choice this is not the problem. The ObjectDisposed exception is triggered by the usage of the variable Form after the exit from the Application.Run call. If you read the docs about Application.Run you will notice a remark that says `The Dispose method of the Form class will be called prior to the return of this method`. – Steve May 18 '15 at 17:55
  • I concur Steve, you are right, maybe I had to mention clearly that the code after the Application.Run had to be eliminated, because the Form would be shown by the Run itself. – Sabrina_cs May 19 '15 at 19:16