开发者

2 events handlers on the same form acting differently

I have 2 event handlers attached to buttons on the same form. I want to disable the form and show a waitCursor while the method is running, then enable the form and put the cursor back to default.

Here's the strange part: with almost the same code, one of these events work, and the other doesn't! What could be wrong开发者_如何学JAVA here?

This one works.

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

While this one throws an error when I try to change the cursor back to default, stating that ActiveForm is 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
}


You don't need to call ActiveForm. Simply using this should work:

Cursor = Cursors.Default;


If you are using only standard Cursor and WaitCursor it is enough to set bool property UseWaitCursor defined at Control.

It seems that inside your code you have your form accessible as 'this'.

Or optionally is the Form accessible if you cast 'sender' to Button(?) and call FindForm() method on the typed result.

And you should add some try/finally block to restore the cursor even in case of exception in your 'processing' code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜