I want to open a new form in C#, so I write:
Form2.Show()
But Visual Studio tells me this error:
An object reference is required for the non-static field, method, or property
Control.Show()
I want to open a new form in C#, so I write:
Form2.Show()
But Visual Studio tells me this error:
An object reference is required for the non-static field, method, or property
Control.Show()
The error is generated because you need to call the Show
method on an instance of Form2
class, which means that you need to create an object and call that method on that object:
Form2 form2 = new Form2();
form2.Show();
I really suggest you to learn the theory of Object Oriented Programming in deep, so that you can actually understand what's going on whenever you might encounter such errors.
When a method is not static, you can only use it on an instance of the class.
Form2 form = new Form2();
form.Show();
Firstly: Reply to the the main questionnaire.
I think you are from Visual Basic .NET background so you are trying to open the form same like Visual Basic .NET way to open form.
In C# you can not open a form like that - you have to make an object of the form first.
Secondly: Reply to answering persons, myForm.show()
is used when we are loading the form in any form container.
Form2 myForm = new Form2();
myForm.ShowDialog();
with the code the form will be opened.