C# DataGridView和MessageBox

C# DataGridView和MessageBox,c#,datagridview,messagebox,C#,Datagridview,Messagebox,我对DGV中的MessageBox有问题。所以,当我单击单元格打开上下文菜单时,下一步我单击此菜单,应该会显示MessageBox,但不会显示出来。为什么? 这是我的代码: private void DGV1_CellClick(object sender, DataGridViewCellEventArgs e) { ContextMenuStrip1.Show(Cursor.Position); } private void optionToolStripMe

我对DGV中的MessageBox有问题。所以,当我单击单元格打开上下文菜单时,下一步我单击此菜单,应该会显示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被隐藏一样。 我正在尝试这个:

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

但仍然不起作用:(

尝试以下方法 这是可行的,因此您的上下文菜单代码和/或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
}
谢谢回复

刚才我正在尝试:(DGV1.Visible=false)


它是有效的。为什么MessageBox没有显示在顶部,而是在DGV下?除了DGV中的数据,我使用了更多的绘画。这是通过这个?奇怪,因为在它工作之前。

这是一个令人沮丧的问题。我有同样的问题,谷歌也没有太多帮助。最终我发现如果你已经实现了e DataGridView(可能还有其他覆盖显示逻辑的事件)消息框在窗口重新绘制之前不会显示在DataGridView上

1) Cheese方法(隐藏和显示网格)

2) 更好的方法(在消息之前分离事件,在消息之后重新连接)

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

也许你需要设置MessageBox的父级?设置一个断点,看看它是否在点击event。ContextMenu通常使用鼠标右键单击,所以我希望看到某种类型的KeyUp或KeyDown或KeyPress事件处理程序
MyGrid.Visible = false;
MessageBox.Show("my message text");
MyGrid.Visible = true;
MyGrid.CellFormatting -= MyGrid_CellFormatting;
MessageBox.Show("my message text");
MyGrid.CellFormatting += MyGrid_CellFormatting;
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();        
    }         
}