Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a message box with details in WinForms?

Just now I noticed that Visual Studio shows a message box with details when a property is set to an invalid value. For example:

Is it possible to make this type of message box in WinForms?

I have tried the following code:

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

But this produced the following error:

Error 24 The best overloaded method match for 'System.Windows.Forms.MessageBox.Show(string, string, System.Windows.Forms.MessageBoxButtons, System.Windows.Forms.MessageBoxIcon, System.Windows.Forms.MessageBoxDefaultButton)' has some invalid arguments

G:\Jagadeeswaran\Nov 17\MCS-SPS School\MCS-SPS School\Certificate\Transfer.cs 164 21 MCS-SPS School

How can I fix this error and get a message box that shows additional details?

like image 825
Sagotharan Avatar asked Dec 28 '11 08:12

Sagotharan


People also ask

How do I display message box?

To display a message box, call the static method MessageBox. Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.

What is message box class explain message box () in detail?

A message box is a prefabricated modal dialog box that displays a text message to a user. You show a message box by calling the static Show method of the MessageBox class. The text message that is displayed is the string argument that you pass to Show.


2 Answers

As others have pointed out, you should write a custom dialog with the desired features. For help on this, you can look at the actual implementation used by the PropertyGrid for this dialog (perhaps with a decompiler) , which is, as of .NET 4.0, the System.Windows.Forms.PropertyGridInternal.GridErrorDlg type, internal to the System.Windows.Forms assembly.

I really wouldn't recommend it (could break in a future release), but if you're feeling really lazy, you can directly use this internal type using reflection.

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

Result:

Details Dialog

like image 138
Ani Avatar answered Oct 17 '22 22:10

Ani


You need to set following properties of Form to create a custom Dialog/Message window.

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

Now, use ShowDialog() method to show custom dialog.

MyDialog dialog=new MyDialog();
DialogResult result=dialog.ShowDialog();
if(result == DialogResult.OK)
{
  //
}

For more information on Dialog read MSDN article - Dialog Boxes (Visual C#)

like image 29
KV Prajapati Avatar answered Oct 17 '22 22:10

KV Prajapati