我有一个问题的MessageBox在DGV。因此,当我单击单元格打开上下文菜单时,接下来我在此菜单中单击,应该会显示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");但还是不能工作:
发布于 2014-06-10 05:17:37
尝试下面这样的代码,这样就可以了,所以也许你的上下文菜单代码和/或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
}发布于 2014-06-11 00:31:15
感谢您的回复!
刚才我试了一下:( 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中的数据外,我更多地使用了绘画。是通过这个吗?很奇怪,因为在它起作用之前。
发布于 2017-07-06 21:44:12
这是一个令人沮丧的问题。我也遇到了同样的问题,谷歌帮不了我多少忙。最后,我发现,如果您已经实现了DataGridView的CellFormatting事件(可能还有覆盖显示逻辑的其他事件),那么在重绘窗口之前,MessageBoxes将不会显示在DataGridView上。要解决此问题,有几种选择,这里有两种。
1) Cheese方法(隐藏和显示网格)
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();
}
}https://stackoverflow.com/questions/24128834
复制相似问题