启用和禁用的形式在C# [英] Enabling and disabling forms in C#

查看:169
本文介绍了启用和禁用的形式在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了两种形式在C#中:form1中的主要应用和窗口2是一个登录窗口。 Form1中有一个按钮,关闭本身并显示窗口2和窗口2有按钮,使主窗体和关闭本身。问题是,当我尝试使用窗口2按钮启用Form1中(我得到类型的未处理的异常System.NullReferenceException发生在Application.exe)

下面是在Form1 code:

 登录登录=新的登录();
Login.Show();
this.Enabled = FALSE;
 

窗口2(登录)code:

 (this.Owner为Form1).Enabled = TRUE; ===>这条线被强调
 关闭();
 

解决方案

您可以使用的 的ShowDialog ,这将在使所有其他形式的inaccessable(同样适用)展示的形式是:

 使用(VAR登录=新的登录()){
    login.ShowDialog(本);
}
 

你还可以通过的 的DialogResult 值,作为返回code,传递成功或失败的调用方法。

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();

解决方案

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.

这篇关于启用和禁用的形式在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆