【发布时间】:2019-12-22 19:02:27
【问题描述】:
我正在编写一个小型 winforms 应用程序,并且我有一个附加到按钮的事件处理程序。在BtnOk_click 事件中,如果文本框的值为 "" 或者它具有默认值,我会检查条件并显示一个消息框。
我正在努力寻找在关闭消息框后退出事件的方法。
我尝试使用 Close(),但我不想关闭整个表单,只是为了退出事件处理程序。
private void BtnOK_Click(object sender, FormClosingEventArgs e)
//when the user presses the OK button on the GUI
{
pcName = txtBoxPcName.Text;
if((pcName == "") || (pcName == "PC Name"))
{
MessageBox.Show("Computer name cannot be \"" + pcName + "\". \nPlease enter a valid computer name!");
if (DialogResult == DialogResult.OK)
{
//how do I exit the event handler and return back to my main form?
}
}
else
{
}
... // some other code continues here
}
在我按下消息框上的 OK 按钮后,代码继续超出 else{} else 条件。
【问题讨论】:
-
只是做一个
return; -
最好在输入有效之前禁用“确定”按钮,而不是发布消息。然后在 UI 上明确说明有效文本应该是什么。