在 C# 中使用 Windows 窗体创建多个窗体?
我想创建两个表单:LoginForm 和 BaseForm。
首先创建 LoginForm,它包含“登录”和“取消”按钮。当用户单击“登录”按钮时,应关闭 LoginForm,并创建并打开 BaseForm。
当用户单击 LoginForm 的“取消”按钮时,它应该关闭。
我该怎么做呢?谁能给我一个好的解决方案吗?
解决方案:
在main()中:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm form=new LoginForm();
if(form.showDialog()==DialogResult.OK)
{
form.close();
Application.Run(new BaseForm());
}
else
form.close();
}
I want to create two forms: LoginForm and BaseForm.
LoginForm is created first and it contains 'Login' and 'cancel' button . When user clicks 'Login' button then LoginForm should be closed and BaseForm should be created and opened.
When user clicks 'cancel' button of LoginForm then it should get closed.
How can I do it? Can anyone please give me good solution for this?
Solution:
In the main():
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm form=new LoginForm();
if(form.showDialog()==DialogResult.OK)
{
form.close();
Application.Run(new BaseForm());
}
else
form.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有一个需要登录的应用程序,我在初始化方法中所做的就是检查用户是否已登录。如果没有,我会打开一个带有登录提示的模态。如果他们取消,我会关闭该应用程序。
显示模式的代码:
登录时的取消按钮将调用类似以下内容的代码:
I have an app that requires login and what I do in the initialization method is check to see if the user is logged in. If they are not I open a Modal with the login prompt. If they cancel that I close the app.
The code to show the modal:
The cancel button on login would call something like:
在应用程序的主函数中:
ShowDialog()
以模式方式显示它。ShowDialog()
返回的值获取请求的操作。Application.Run()
。如果您按此顺序执行此操作,那么直到您的用户完成登录表单后才会显示主表单。
In your application's main function:
ShowDialog()
to show it modally.ShowDialog()
.Application.Run()
.If you do it in this order then the main form won't be shown until your user has finished with the login form.
您只需打开主窗体,检查用户是否已通过身份验证。然后显示登录表单,如果用户取消登录表单,只需关闭应用程序即可。
You could simply open the main form, check if the user is authenticated. Then show the login form and if the user cancels the login form, just close the application.