2011-09-21 73 views
2

我有2個事件處理程序附加到同一表單上的按鈕。我想禁用窗體並在方法運行時顯示一個waitCursor,然後啓用窗體並將光標恢復爲默認值。相同形式的2個事件處理程序的行爲方式不同

以下是奇怪的部分:使用幾乎相同的代碼,其中一個事件起作用,另一個不起作用!這裏有什麼可能是錯的?

這一個工程。

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 
} 

回答

1

您無需致電ActiveForm。只需用這應該工作:

Cursor = Cursors.Default; 
+0

- This works!你知道爲什麼嗎?你能解釋爲什麼嗎?我在這裏很神祕。 – MAW74656

+1

「WIP」報告將焦點從您的應用程序中抽離出來。所以ActiveForm因爲null,沒有表單處於活動狀態。 –

+0

好的,但爲什麼WIP做到這一點,並且Exception Report沒有? – MAW74656

0

如果您僅使用標準光標和WaitCursor它足以設定在控制定義布爾屬性UseWaitCursor。

看起來,在你的代碼中,你的表單可以用'this'訪問。

或者可選地,如果您將「發件人」轉換爲Button(?)並對類型化的結果調用FindForm()方法,則表單可以訪問。

你應該添加一些try/finally塊來恢復遊標,即使在你的「處理」代碼中有例外

相關問題