2014-06-09 27 views
1

我在DGV中的MessageBox有問題。所以,當我點擊單元格打開上下文菜單時,接下來我點擊這個菜單並且應該顯示MessageBox,但是不顯示。爲什麼?DataGridView和MessageBox

這是我的代碼:

private void DGV1_CellClick(object sender, DataGridViewCellEventArgs e) 
    { 
     ContextMenuStrip1.Show(Cursor.Position); 
    } 

private void optionToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     DialogResult res = MessageBox.Show("Are You Sure?", 
       "Are You Sure", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
     if (res == DialogResult.Yes) 
     { 
      ...... 
     } 
    } 

此消息框顯示不出來,但在應用程序沒有什麼可以做,因爲如果在MessageBox被隱藏。 我想這一點:

MessageBox.Show(new Form { TopMost = true }, "Message"); 

但仍然沒有工作:(

+0

也許你需要設置MessageBox的父級? –

+0

放置一個斷點,看看它是否觸及事件 – MethodMan

回答

0

嘗試類似如下的下方 這工作那麼也許有一個在您的ContextMenu代碼中的問題,或DGV事件可以提供你。?!更多的代碼

DialogResult dialogResult = MessageBox.Show("Are You Sure?", "Are You Sure", MessageBoxButtons.YesNo); 
if(dialogResult == DialogResult.Yes) 
{ 
    //do something 
} 
else if (dialogResult == DialogResult.No) 
{ 
    //do something else 
} 
+0

另外ContextMenu的工作通常是右鍵點擊鼠標,所以我期待看到某種KeyUp或KeyDown或KeyPress事件處理程序 – MethodMan

0

感謝您的回覆

剛纔我想這樣的:(DGV1.Visible = FALSE)

private void optionToolStripMenuItem_Click(object sender, EventArgs e) 
{ 

    DGV1.Visible = false 

    DialogResult res = MessageBox.Show("Are You Sure?", "Are You Sure", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
     if (res == DialogResult.Yes) 
     { 
      ...... 

    } 
} 

它的工作原理。爲什麼MessageBox沒有顯示在頂部,但在DGV下?除了我的dgv中的數據外,我使用了更多的繪畫。這是通過這個?奇怪,因爲在它工作之前。

0

這是令人沮喪的問題。我有這個相同的問題,谷歌並沒有太多的幫助。最終我發現,如果您已經實現了DataGridView的CellFormatting事件(也可能是其他重寫顯示邏輯的事件),則在重繪窗口之前,MessageBoxes不會顯示在DataGridView上。要解決此問題,有幾個選項,這裏有兩個選項。

1)奶酪方法(隱藏和顯示網格)

MyGrid.Visible = false; 
MessageBox.Show("my message text"); 
MyGrid.Visible = true; 

2)更好的方法(消息之前,分離的事件,之後重新連接)

MyGrid.CellFormatting -= MyGrid_CellFormatting; 
MessageBox.Show("my message text"); 
MyGrid.CellFormatting += MyGrid_CellFormatting; 

3),將其套在您的作爲幫手的形式

private DialogResult ShowMessageBox(string p_text, string p_caption, MessageBoxButtons p_buttons, MessageBoxIcon p_icon) { 
    bool detached = false; 
    try {     
     // detach events 
     MyGrid.CellFormatting -= MyGrid_CellFormatting;   
     detached = true; 
     // show the message box 
     return MessageBox.Show(p_text, p_caption, p_buttons, p_icon); 
    } 
    catch(Exception ex) { 
     throw ex; 
    } 
    finally { 
     if(detached) { 
      // reattach 
      MyGrid.CellFormatting += MyGrid_CellFormatting;    
     }   
     MyGrid.Invalidate();   
    }   
}