为什么DialogResult = DialogResult.OK不起作用

waiyuyi 2012-04-05 09:38:53
我调用了一个函数,然后再调用这个,点击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;
}
...全文
648 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
深海之蓝 2012-04-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
你跟踪一下,因为你的OnCheck函数返回true了,if条件成立了,所以执行return;了,下面的代码不执行了...
[/Quote]
人家都告诉你加断点跟踪了,你就跟踪一下,别人家说的答案,你改了不成功再来反馈一遍,这样学不到东西的。要学会自己思考。
waiyuyi 2012-04-05
  • 打赏
  • 举报
回复
我把Oncheck()函数最后的return true,改成return false,结果还是一样
else if(2 == nType)
{
if (!OnCheck())
{
return ;
}
else
{
InitAdmin();
SQLServer.ModifyOperator(m_Admin);
this.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 false;
}
那该如何改啊
gxingmin 2012-04-05
  • 打赏
  • 举报
回复
你跟踪一下,因为你的OnCheck函数返回true了,if条件成立了,所以执行return;了,下面的代码不执行了...
waiyuyi 2012-04-05
  • 打赏
  • 举报
回复
我把OnCheck()中的代码直接放进去是可以实现的,这是为什么呢?
waiyuyi 2012-04-05
  • 打赏
  • 举报
回复
还是不可以,改了之后还是同样的效果
孟子E章 2012-04-05
  • 打赏
  • 举报
回复
DialogResult dr = new DialogResult ();
dr = frm2.ShowDialog();
if ( dr == DialogResult.OK )
gxingmin 2012-04-05
  • 打赏
  • 举报
回复
OnCheck函数应该改造一下,MessageBox后面加上return false;

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;
}
orochiheart 2012-04-05
  • 打赏
  • 举报
回复
this.DialogResult = DialogResult.ok;
gxingmin 2012-04-05
  • 打赏
  • 举报
回复
OnCheck函数一直都是返回true,也就是说代码执行到
if (OnCheck())
{
return;
}
就返回了,下面的else代码不可能会执行到

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧