我知道,我可以事先显示这种形式显示(负载()事件,IIRC)在事件的登录表单劫持的形式,但在(也许愚蠢)试图"走捷径",创造一个快速和肮脏的登录画面,我陷入两难境地.
这是我对现有项目所做的事情(事后添加的登录表单):
1) Created the login form
2) Changed program.cs so that this login form is now the first form created
3) Added code to the login form that shows the "main" form if the login is successful. I then Hide the login form.
Run Code Online (Sandbox Code Playgroud)
这导致应用程序永远不会关闭(除了通过Shift + F5),因为隐藏的主要表单仍然潜伏着.所以,我在"main"表单的FormClosing事件中添加了一个"Close()",认为这会导致整个应用程序关闭(IOW,隐藏登录表单).
但是(也许是合适的),而不是解决我的问题,这会导致"System.Windows.Forms.dll中发生类型'System.StackOverflowException'的未处理异常"
现在我不知道我是否应该继续这种快速和肮脏的尝试(以及如何)或减少我的损失并恢复到show-the-login-form-in-the-Load事件方法[ology].
Alex M的解决方案奏效了.您需要做的唯一事情是在您的登录表单中这样的事情:
private void buttonLogin_Click(object sender, EventArgs e)
{
String userName = textBoxUserName.Text.Trim();
String pwd = textBoxPassword.Text.Trim();
if ((userName == "donMcLean") || (pwd == "Drove my Chevy to the levee but the levee was dry, them good ole boys were drinking whiskey & Rye"))
{
this.DialogResult = DialogResult.OK;
}
else
{
MessageBox.Show("Incorrect User Name and/or Password");
}
}
Run Code Online (Sandbox Code Playgroud)
创建顺序并不是特别重要.您可以使用显示登录表单form.ShowDialog().这样做可以处理表单并解决您所描述的问题.
例如:
var login = new LoginForm();
var mainForm = new MainForm();
if (login.ShowDialog() != DialogResult.Ok)
{
return; // Exit the application
}
Application.Run(mainForm);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2164 次 |
| 最近记录: |