简体   繁体   中英

How to show message box with details and OK option

I want to show a message box with Ok and Detail button in form.I have done some code for this functionality but it also shows a cancel button and in detail section it show only that message which i have written in string. Code is

var dialogTypeName = "System.Windows.Forms.PropertyGridInternal.GridErrorDlg";
var dialogType = typeof(M3Form).Assembly.GetType(dialogTypeName);

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


// Populate relevant properties on the dialog instance.
dialog.Text = "Accknowledge Error";
dialogType.GetProperty("Details").SetValue(dialog, "Maximum length has been exceeded. Maximum = 50, Actual =" + txtName.Text.Length + "", null);
dialogType.GetProperty("Message").SetValue(dialog, "Maximum length has been exceeded. Maximum = 50, Actual =" + txtName.Text.Length + "", null);

// Display dialog.
var result = dialog.ShowDialog();

How can i show only OK button and detail button with stack trace error message in detail section.

If I understand correctly, you want to change the default OK and Cancel buttons on a MessageBox to OK and Details . This requires too much work and isn't something you can easily do without creating your own MessageBox.

IMO you'd be better off creating a new Form that looks like a MessageBox and show that each time you need to display a message. You can then use ShowDialog when showing your MessageBox Form so that it's a modal window similar to a MessageBox.

I know this is an old thread and you might have found an answer already. But just wanted to document my answer for anyone's use. As pointed out by Christian, using internal classes of .NET framework is not wise. But if you really wanna do it, you can do it this way.

var cancelBtn = dialog.Controls.Find("cancelBtn", true);
cancelBtn[0].Visible = false;

Though this will hide the Cancel button, this will leave the OK button hanging around in the centre of the dialog. So if you want to display only OK button and if you don't necessarily care about the dialog result you can do it the following way:

var okBtn = dialog.Controls.Find("okBtn", true);
okBtn[0].Visible = false;

var cancelBtn = dialog.Controls.Find("cancelBtn", true);
cancelBtn[0].Text = okBtn[0].Text;

You are free to explore more customization on the form, as you have got the Form object.

This is an internal class (hence the namespace name and the fact that it is internal and you even need reflection to access it in the first place).

Using internal classes of the .NET framework is not very wise. They might change (or disappear) without notice. Also, asking for "how does it work" or even documentation is rather pointless.

Consider creating your own dialog like @keyboardP suggested, or look into the standard Windows Task Dialog component (which is available from Windows Vista onwards). TaskDialog is a native component and you (ultimately) need P/Invoke to use it. That work has been done (multiple) times however, just search Google or SO for TaskDialog .NET .

Using a TaskDialog has the additional benefit that it is recognizable by your users, as Windows itself uses it all over the place.

If you have issues / restrictions using P/Invoke you could also choose one of the re-implementations in managed code .

Try this

// 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 = "Data Patch";
dialogType.GetProperty("Details").SetValue(dialog, "Sample Text", null);
dialogType.GetProperty("Message").SetValue(dialog, "Sample Text", null);

// Display dialog.
var result = dialog.ShowDialog();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM