我在我的应用程序中使用了继承自Messagebox
类的自定义Form
。当我在主表单上使用它时,它工作正常。但是当我在自己从主窗体弹出的表单上使用其Show()
函数时,Messagebox
隐藏在第二个表单下,因此程序变得不可用。
即使我在BringToFront()
之前使用ShowDialog()
函数,它仍然会回来。
这是此自定义Messagebox的Show()
功能。如有必要,我可以分享更多代码:
public static DialogResult Show(string message, string title)
{
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox.Size = MsgBox.MessageSize(message);
MsgBox.InitButtons(Buttons.OK);
//_msgBox.BringToFront();
_msgBox.ShowDialog();
return _buttonResult;
}
MsgBox
是类本身的名称:
class MsgBox : Form
答案 0 :(得分:4)
尝试传递内部消息框类的所有者值
public static DialogResult Show(string message, string title, Form owner = null)
{
_msgBox = new MsgBox();
_msgBox._lblMessage.Text = message;
_msgBox._lblTitle.Text = title;
_msgBox.Size = MsgBox.MessageSize(message);
MsgBox.InitButtons(Buttons.OK);
if(owner != null)
_msgBox.ShowDialog(owner);
else
_msgBox.ShowDialog();
return _buttonResult;
}
使用默认参数,您只能在需要的位置更改代码。
经过一番研究后,我发现this question and its answers解释了这种行为