c# - 同一表单上的 2 个事件处理程序表现不同

标签 c# winforms events

我有 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;
}

当我尝试将光标更改回默认值时,此错误会引发错误,并指出 ActiveFormnull

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/

相关文章:

c# - printdialog.showdialog();在 64 位 Windows 7 中不显示打印对话框

c# - 使用 BeginInvoke 运行委托(delegate)方法

Windows 的 C++ 事件跟踪 (ETW) 包装器

绑定(bind)完成时的 .NET 事件

c# - 针对 x86 和 x64 的设置?

c# - WinForms 的免费或开源图表组件

C# Gecko 选择特定的列表框或组合框

javascript - 在捕获和冒泡阶段调用处理函数时,event.target 是什么?

c# - 如何在 C# 控制台应用程序中输出 Windows Alt 键码

c# - 是否有与 .net 的纯基于文件的 MSSQL 数据库等效的 PHP/Java?