2009-09-23 72 views
0

我正在爲圖書館管理系統創建應用程序。禁用關閉按鈕,直到用戶單擊LogOff MenuItem

我想先關閉關閉按鈕,並在用戶點擊菜單 項目註銷後啓用它。

有沒有什麼辦法可以在我的應用程序中完成此功能?

我嘗試在formClosing事件中使用下面的代碼,但它不工作。

private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e) 

    { 

    e.Cancel = false; 

    if (checkLogOff == false) 

    { 

    MessageBox.Show("Please Log Off before closing the Application"); 

    e.Cancel = false; 

    this.ControlBox = false; 

    return; 

    } 

    else 

    { 

    this.ControlBox = true; 

    e.Cancel = true; 

    } 

    } 

checkLogOff變量的值設置如下:

public bool checkLogOff = false; 

    private void logOffToolStripMenuItem_Click(object sender, EventArgs e) 

    { 

    checkLogOff = true; 

    /*Code to update the logoff users in the database*/ 

    } 

在執行應用程序,如果我不點擊我收到的對話框中,但在註銷菜單項後,我立刻按下OK應用程序關閉的消息框中的按鈕。但我不想讓用戶在單擊LogOff MenuItem之前關閉應用程序。

請幫我完成這個任務。

在此先感謝!

+0

嗯,爲什麼不在關閉表單時將人員關閉?還是不夠辛苦? – Will 2009-09-23 11:56:57

回答

0

當checkLogOff == false時,您要取消該事件,因此您應該將e.Cancel設置爲True, ,並在checkLogOff == true時將其設置爲False。

+0

感謝您的幫助現在它工作正常! – Sheetal 2009-09-23 11:08:28

1

爲了達到這個目的,我不想擺弄ControlBox屬性,因爲它也刪除了最大化和最小化按鈕。你的代碼的主要問題是你應該在FormClosing中設置Canceltrue以防止表單被關閉。我覺得你的代碼可以減少到這一點,而且還能達到你想要的(因爲我們不碰ControlBox):

private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    e.Cancel = !checkLogOff; 
    if (e.Cancel) 
    { 
     MessageBox.Show("Please Log Off before closing the Application"); 
    } 
} 
+0

感謝您的幫助現在它工作正常! 即使你的回答是正確的,但我可以標記只有一個人的答案是正確的答案,所以很抱歉。無論如何非常感謝您的及時幫助! – Sheetal 2009-09-23 11:09:19

0

你倒e.Cancel

的設置

它必須是:

e.Cancel = true; 

當您需要阻止應用程序關閉。

==>

private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     e.Cancel = false; 
     if (checkLogOff == false) 
     { 
      MessageBox.Show("Please Log Off before closing the Application"); 
      e.Cancel = true; // <-- 
      this.ControlBox = false; 
      return; 
     } 
     else 
     { 
      this.ControlBox = true; 
      e.Cancel = false; // <-- 
     } 
    } 
1

更改此:

MessageBox.Show("Please Log Off before closing the Application"); 
e.Cancel = false; 
this.ControlBox = false; 
return; 

要這樣:

MessageBox.Show("Please Log Off before closing the Application"); 
e.Cancel = true; 
this.ControlBox = false; 
return; 

你是不是取消形式負載。

0

我知道這不是你所尋求的答案,而是告訴用戶去另一個菜單項登錄,如果他只是想關閉應用程序是cubersome。

爲什麼告訴他他需要註銷?不能只爲他代表註銷功能,或者顯示一個對話框詢問他是否要註銷?

+0

感謝您的建議我已經在單獨的方法中包含了LogOff的編碼,並在用戶單擊表單的關閉按鈕時自動調用它。 非常感謝。 – Sheetal 2009-09-23 11:23:57

0

我強烈建議您對e.CloseReason進行測試,並且只執行取消邏輯,如果它是用戶關閉操作,否則您的應用程序將阻止系統關閉,並立即結束此任務對話框對用戶來說不是一個好印象。