为什么DialogResult = DialogResult.OK不起作用
我调用了一个函数,然后再调用这个,点击OK没反应,代码如下
else if(2 == nType)
{
if (OnCheck())
{
return;
}
// else
// {
InitAdmin();
SQLServer.ModifyOperator(m_Admin);
DialogResult = DialogResult.OK;
// }
}
private bool OnCheck()
{
if ("" == textName.Text.Trim())
{
MessageBox.Show("用户名不能为空!", "提示");
}
else if ("" == textPsw.Text.Trim())
{
MessageBox.Show("密码不能为空!", "提示");
}
else if ("" == textPsw2.Text.Trim())
{
MessageBox.Show("重复密码不能为空!", "提示");
}
else if (textPsw.Text != textPsw2.Text)
{
MessageBox.Show("密码和重复密码不相同,请重新输入", "提示");
textPsw.Text = "";
textPsw2.Text = "";
}
return true;
}
[解决办法]
OnCheck函数应该改造一下,MessageBox后面加上return false;
- C# code
private bool OnCheck() { if ("" == textName.Text.Trim()) { MessageBox.Show("用户名不能为空!", "提示");return false; } else if ("" == textPsw.Text.Trim()) { MessageBox.Show("密码不能为空!", "提示"); return false; } else if ("" == textPsw2.Text.Trim()) { MessageBox.Show("重复密码不能为空!", "提示");return false; } else if (textPsw.Text != textPsw2.Text) { MessageBox.Show("密码和重复密码不相同,请重新输入", "提示"); textPsw.Text = ""; textPsw2.Text = ""; return false; } return true; }
[解决办法]
你跟踪一下,因为你的OnCheck函数返回true了,if条件成立了,所以执行return;了,下面的代码不执行了...