C#中显示Windows窗体 [英] c# Show Windows Form

查看:508
本文介绍了C#中显示Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



它告诉我,一个对象引用所需的非静态字段,方法或属性 System.Windows.Forms.Form.ShowDialog()

it tells me that "An object reference is required for the non-static field, method or property 'System.Windows.Forms.Form.ShowDialog()'

在过去,我可以简单地使用上面的代码片段显示形式。所以,很明显的东西自从上次我写了一个Windows控制台应用程序已经改变了。

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

这当然, ,是一个空白表单。这样看来,你的类型被称为 frmLogin ,通常这听起来像一个变量名,而不是一个类名,但这个错误你要在这里告诉我,这是一个类:

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

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

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

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

这篇关于C#中显示Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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