我在c#中用一些控件写了一个表单。当我们离开时,在其中一个控件上有一个事件被调用。在某些情况下,当框中的值无效时,它会抛出异常(如果bool变量为true)。现在的问题是,当焦点在这个控件上并且我想要关闭表单时,将调用离开此控件的事件并抛出异常,这是catch语句中的catch并且不会抛出任何异常但是在此之后onclose事件是不会打电话,我不能关闭表格。我该怎么办?
private void SetSelectedDateTime(CancelEventArgs e, bool raiseException)
{
try
{
var newval = UpdateDateTime();
var rawText = Text.Replace("/", "").Replace(":", "").Replace("_", "").Replace(" ", "");
if ((Text.Contains(PromptChar) && rawText != string.Empty) || (!Text.Contains(PromptChar) && newval == null))
{
throw new SgException(ControlMessages.InvalidDate);
}
else
{
selectedDateTime = AdjustDateTime(selectedDateTime);
if (newval != selectedDateTime)
{
selectedDateTime = newval;
UpdateText(newval);
OnSelectedDateTimeChanged();
EndEditBindings();
}
}
}
catch (Exception)
{
e.Cancel = true;
if (raiseException)
{
this.Focus();
throw;
}
}
}
答案 0 :(得分:1)
你应该删除这一行
throw;