c#显示Windows窗体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19939256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:10:18  来源:igfitidea点击:

c# Show Windows Form

c#.netwinforms

提问by Skittles

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;

所以,我在这里有点挣扎。我正在用 C# 编写一个 Windows 控制台应用程序,并且刚刚为名为 frmLogin 的应用程序制作了一个登录表单。我尝试使用 MS 记录的方法;

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

它告诉我“非静态字段、方法或属性‘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.

在过去,我可以通过简单地使用上面的代码片段来显示表单。所以,很明显,自从我上次编写 Windows 控制台应用程序以来,事情已经发生了变化。

Can someone show me the error of my ways?

有人可以告诉我我的方式的错误吗?

采纳答案by David

This creates a new instance of type Form:

这将创建一个新的 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. 通常这听起来像一个变量名而不是一个类名,但是你在这里得到的错误告诉我它是一个类:

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 highlyrecommend renaming the form itself to something like:

但是,为了符合命名标准和约定(以帮助防止将来出现混淆和问题),我强烈建议将表单本身重命名为:

LoginForm

Then you can use something like frmLoginas the variable name, which is a much more common approach:

然后你可以使用类似的东西frmLogin作为变量名,这是一种更常见的方法:

LoginForm frmLogin = new LoginForm();
frmLogin.ShowDialog();

回答by Mike Perrenoud

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:

问题在于,使用您从 Microsoft 获取的代码片段,您只是在构建基本类型。你需要建立你的表格。所以,而不是new Form,你建立一个new frmLogin

var f = new frmLogin();
f.ShowDialog();

回答by Brian

If this doesn't solve your problem, I will delete my answer but, I am assuming that you aren't instantiating your form:

如果这不能解决您的问题,我将删除我的答案,但是,我假设您没有实例化您的表单:

Form2 frmLogin = new Form2();
frmLogin.ShowDialog();

This works just fine for me. Although personally, I would simply use the .Show()method:

这对我来说很好用。虽然就个人而言,我只会使用以下.Show()方法:

Form2 frmLogin = new Form2();
frmLogin.Show();

回答by Syed Shayan Ali Shah

Create the object of the FORM you are trying to open for instance "frmStudent"

创建您尝试打开的 FORM 对象,例如“frmStudent”

Method 1:

方法一:

frmStudent obj = new frmStudent();
obj.Show();

Method 2:

方法二:

It will open the form , but you can't move to another form until you close it.

它将打开表单,但在关闭它之前您无法移动到另一个表单。

frmStudent obj = new frmStudent();
obj.ShowDialog();