【发布时间】:2010-10-12 09:19:08
【问题描述】:
当用户单击保存并且列表框中没有任何内容时,我想引发错误。 我想我会像这样使用 try catch 块:
try
{
//when you go to save, and the list box is empty, you should get an error message
if (dataListBox.Items.Equals(null))
throw new Exception();
//i wanted to save on the form_close event, so i put the save data into a method and just call the method from this event
this.save();
}
catch (Exception err)
{
//spits out the errors if there are any
MessageBox.Show(err.Message, "List Box is empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
但这对我不起作用。它仍然保存并且没有消息框出现。
【问题讨论】: