So, I'm struggling a little bit here. I am writing a windows console application in C# and have just made a login form for the application called frmLogin. I tried using the MS documented method of;
Form f = new Form();
f.ShowDialog();
but this obviously loads/displays a blank form and not the form I defined in the form designer.
In my main application, I want to be able to show the login form programmatically, but when I try to use;
frmLogin.ShowDialog();
it tells me that "An object reference is required for the non-static field, method or property 'System.Windows.Forms.Form.ShowDialog()'
In the old days, I could show a form by simply using the above snippet of code. So, obviously something has changed since the last time I wrote a windows console app.
Can someone show me the error of my ways?
This creates a new instance of type Form
:
Form f = new Form();
Which, of course, is a blank form. It would appear that your type is called frmLogin
. Normally this sounds like a variable name and not a class name, but the error you're getting here tells me that it's a class:
frmLogin.ShowDialog();
Given that, then the quickest way to solve your problem would be to create an instance of your form and show it:
frmLogin login = new frmLogin();
login.ShowDialog();
However, in keeping with naming standards and conventions (to help prevent future confusion and problems), I highly recommend renaming the form itself to something like:
LoginForm
Then you can use something like frmLogin
as the variable name, which is a much more common approach:
LoginForm frmLogin = new LoginForm();
frmLogin.ShowDialog();
The problem is that with the code snippet you took from Microsoft, you were just building the base Type. You need to build your form. So instead of new Form
, you build a new frmLogin
:
var f = new frmLogin();
f.ShowDialog();
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