Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.
" + 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:JagadeeswaranNov 17MCS-SPS SchoolMCS-SPS SchoolCertificateTransfer.cs 164 21 MCS-SPS School

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
546 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...