Reputation: 121
I have created two forms in C#: form1 is the main application and form2 is a logon window. form1 has a button that disable itself and show form2 and form2 has button that enable the main form and closes itself. The problem is when I try to enable form1 using a form2 button (I get "An unhandled exception of type 'System.NullReferenceException'
occurred in Application.exe")
Here is the form1 code:
Login Login = new Login();
Login.Show();
this.Enabled = false;
form2(Login) code:
(this.Owner as Form1).Enabled = true; ===> this line gets highlighted
Close();
Upvotes: 0
Views: 630
Reputation: 52538
You could use ShowDialog
, this will make all other forms (of the same application) inaccessable during the show of the form:
using (var login = new Login()) {
login.ShowDialog(this);
}
And you can pass one of the DialogResult
values, as the return code, to pass success or failure to the calling method.
Upvotes: 1
Reputation: 26886
Change this line of code in form1
from
Login.Show();
to
Login.Show(this);
or set owner explicitly before showing like:
Login.Owner = this;
Login.Show();
Otherwise that login form will not has owner and this.Owner
will be null leading to NullReferenceException
when you're trying to access its members.
Upvotes: 5