如何在WinForms中显示包含详细信息的消息框?

Sag*_*ran 17 .net c# messagebox winforms

刚才我注意到当一个属性设置为无效值时,Visual Studio会显示一个带有详细信息的消息框.例如:

是否可以在WinForms中生成这种类型的消息框?

我试过以下代码:

MessageBox.Show("Error in Division Fill.\n" + ex.Message,
                "Information",            
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxOptions.RightAlign);
Run Code Online (Sandbox Code Playgroud)

但是这产生了以下错误:

错误24'System.Windows.Forms.MessageBox.Show(string,string,System.Windows.Forms.MessageBoxButtons,System.Windows.Forms.MessageBoxIcon,System.Windows.Forms.MessageBoxDefaultButton)'的最佳重载方法匹配有一些无效的参数

G:\ Jagadeeswaran\11月17日\ MCS-SPS学校\ MCS-SPS学校\证书\ Transfer.cs 164 21 MCS-SPS学校

如何修复此错误并获取显示其他详细信息的消息框?

Ani*_*Ani 25

正如其他人指出的那样,您应该编写一个包含所需功能的自定义对话框.有关这方面的帮助,您可以查看PropertyGrid此对话框(可能使用反编译器)使用的实际实现,从.NET 4.0开始,它是程序集System.Windows.Forms.PropertyGridInternal.GridErrorDlg内部的类型System.Windows.Forms.

真的不推荐它(可能会在将来的版本中破坏),但是如果你感觉很懒,你可以直接使用这种内部类型使用反射.

// Get reference to the dialog type.
var dialogTypeName = "System.Windows.Forms.PropertyGridInternal.GridErrorDlg";
var dialogType = typeof(Form).Assembly.GetType(dialogTypeName);

// Create dialog instance.
var dialog = (Form)Activator.CreateInstance(dialogType, new PropertyGrid());

// Populate relevant properties on the dialog instance.
dialog.Text = "Sample Title";
dialogType.GetProperty("Details").SetValue(dialog, "Sample Details", null);
dialogType.GetProperty("Message").SetValue(dialog, "Sample Message", null);

// Display dialog.
var result = dialog.ShowDialog();
Run Code Online (Sandbox Code Playgroud)

结果:

详细信息对话框

  • 如果我不想在此消息框中取消选项,我该怎么办? (2认同)

ada*_*ost 10

您需要设置Form的以下属性以创建自定义对话框/消息窗口.

  1. 的AcceptButton
  2. CancelButton
  3. FormBorderStyle = FixedDialog
  4. MaximizeBox = FALSE
  5. MinimizeBox = FALSE
  6. ShowIcon =假
  7. ShowInTaskBar =假
  8. 中StartPosition = CenterParent

现在,使用ShowDialog()方法显示自定义对话框.

MyDialog dialog=new MyDialog();
DialogResult result=dialog.ShowDialog();
if(result == DialogResult.OK)
{
  //
}
Run Code Online (Sandbox Code Playgroud)

有关Dialog read MSDN文章的更多信息 - 对话框(Visual C#)