gpt4 book ai didi

c# - 如何正确打开登录表单

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:17 24 4
gpt4 key购买 nike

我的 C# Windows 应用程序中有一个登录表单和 MDI 主表单。因为我在 MDI 表单加载事件中像这样打开我的登录表单。当登录成功时,它只会退出并启用 MDI 主窗体。最近我才发现,如果我关闭我的登录表单,它就会关闭,然后它会毫无障碍地启用我的 MDI 主界面。

这就是我在 MDI 主窗体中加载登录信息的方式。

private void MDiMain_Load(object sender, EventArgs e)
{
setDisplaysize();

Form newLogin = new FormControllers.FrmLogin();
newLogin.StartPosition = FormStartPosition.CenterScreen;
//newLogin.Show(this);
newLogin.ShowDialog(this);
newLogin.Focus();
newLogin.TopMost = true;
newLogin.Activate();

}

然后我尝试使用此代码段像这样更改我的应用程序

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FormControllers.FrmLogin fLogin = new FormControllers.FrmLogin();
if (fLogin.ShowDialog() == DialogResult.OK)
{

Application.Run(new MDiMain());
}
else
{
Application.Exit();
}
}

现在窗体登录打开,但在成功登录后 MDI 主窗体没有启动。我在这里做错了什么?

此外,这是我在登录表单中用于登录按钮的代码

private void btnLogin_Click(object sender, EventArgs e)
{
string txtPass = "";
string txttPassword = "";
string txtHoldStr = "";
String txtStringst1 = "";
char chrFstep ='c';
char chrSstep ='c';
int testInt = 0;

using (DataControllers.RIT_Allocation_Entities EntityModel = new DataControllers.RIT_Allocation_Entities())
{
try
{
userHeadModel = EntityModel.TBLU_USERHED.Where(x => x.USERHED_USERCODE == (txtUserName.Text.Trim())).FirstOrDefault();
txtPass = userHeadModel.USERHED_PASSWORD;
txttPassword = txtPassword.Text.Trim();

if (txtPass == txtHoldStr)
{
MessageBox.Show("Login Successful");
this.Close();
}
else
{
MessageBox.Show("Invalid username or password please try again");
txtPassword.Focus();
}
}
catch (Exception ex) { }

}
}

最佳答案

您需要设置对话结果:

if (txtPass == txttPassword)
{
MessageBox.Show("Login Successful");
DialogResult = DialogResult.OK;
Close();
}

只有默认按钮会自动为您执行此操作。当涉及到逻辑时,您需要根据身份验证的结果(在本例中为身份验证)对其进行设置。

除此之外,我猜测与原始代码中的 txtHoldStr 的比较是错误的。此变量始终为空。要检查文本框中的密码是否与数据模型中的密码匹配,请将 txtPass 与 txttPassword 进行比较。

关于c# - 如何正确打开登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53744918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com