我有 2 个事件处理程序附加到同一表单上的按钮。我想在方法运行时禁用表单并显示 waitCursor,然后启用表单并将光标恢复为默认值。
这是奇怪的部分:使用几乎相同的代码,其中一个事件起作用,而另一个不起作用!这里可能出了什么问题?
这个有效。
private void btnExceptionReport_Click(object sender, EventArgs e)
{
lblStatus.Text = "Printing exception report.";
ActiveForm.Cursor = Cursors.WaitCursor;
//Form.ActiveForm.Enabled = false;
if (DatabaseOps.printItemReport("Exceptions", cboEmployee.Text))
{
lblStatus.Text = "Exception report printed.";
}
else
{
MessageBox.Show("Error printing exception report.");
lblStatus.Text = "Error printing Exception report.";
}
//Form.ActiveForm.Enabled = true;
ActiveForm.Cursor = Cursors.Default;
}
当我尝试将光标更改回默认值时,此错误会引发错误,并指出 ActiveForm
为 null
。
private void btnWIPReport_Click(object sender, EventArgs e)
{
lblStatus.Text = "Printing WIP Materials report.";
ActiveForm.Cursor = Cursors.WaitCursor;
//Form1.ActiveForm.Enabled = false;
if (DatabaseOps.printItemReport("WIP", cboEmployee.Text))
{
lblStatus.Text = "WIP Materials report printed.";
}
else
{
MessageBox.Show("Error printing WIP Materials report.");
lblStatus.Text = "Error printing WIP Materials report.";
}
//Form1.ActiveForm.Enabled = true;
ActiveForm.Cursor = Cursors.Default; //This line gives error saying ActiveForm is null
}
最佳答案
您不需要调用 ActiveForm
。简单地使用它应该工作:
Cursor = Cursors.Default;
关于c# - 同一表单上的 2 个事件处理程序表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7505335/