C# 中的两种不同的窗口形式
我是 C# 和 .NET 编程新手。 我想设计一个打开一个小登录屏幕的应用程序,当用户按下“登录”按钮时,我的程序应该关闭登录表单并传递到一个新表单。 我怎样才能实现这个简单的过程?
谢谢。
I am new to C# and .NET programming. I want to design an application that opens up with a small login screen and when user presses the "Login" button, my program should close the login form and pass to a new form. How can I achieve this simple process?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能是一个解决方案;
在登录表单中;
在程序.cs中
This could be a solution;
In LoginForm;
In program.cs
根据应用程序的整体架构,我经常喜欢让主窗体控制启动登录屏幕。
Depending on the overall architecture of your application, I often like to let the main form control launching the login screen.
这里的挑战是您传递给 Application.Run() 方法的形式。 如果您使用登录表单的实例启动应用程序,然后关闭该表单,我相信应用程序将退出。 毫无疑问,有多种方法可以处理这个问题...
一种方法是将主窗体的实例传递给 Application.Run 方法(这将消息循环与该窗体实例而不是登录窗体联系起来)。 在主窗体的 OnLoad 方法中,您可以使用模式对话框来执行登录。 IE
The challenge here is what form you pass to the Application.Run() method. If you start the application with an instance of the login form and then close that form I believe the application will exit. No doubt there are several ways to handle this...
One way is to pass an instance of your main form to the Application.Run method (this ties the message loop to that form instance and not the login form). In the OnLoad method of the main form you can use a modal dialog to perform the login. i.e.
如果您确实使用 .ShowDialog(),请不要忘记将其包装在 using 块中,或者在完成后在登录表单上使用 .Dispose。 模型对话框必须手动处理。
If you do use .ShowDialog(), don't forget to wrap it around an using block, or use .Dispose on the login form after you are done. Model dialogs must be manually disposed.