Do you have an example the shows how to use the MessageBox.Show method with an event handler for the result?
I am trying to port this code:
MessageBox.Show(“Are you sure you want to delete folder ‘” +
this.treeView1.SelectedNode.FullPath + “‘?”,
“Confirm Delete Folder”,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
new EventHandler(DeleteFolderMBClosed));
private void DeleteFolderMBClosed(object sender, EventArgs e)
{
try
{
DialogResult dr = ((MessageBoxWindow)sender).DialogResult;
if (dr == DialogResult.Yes)
{
DirectoryInfo oDirinfo = this.treeView1.SelectedNode.Tag as DirectoryInfo;
oDirinfo.Delete(true);
this.LoadTreeView();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
the Wisej messagebox has an Action parameter, but I am not sure how to use it.
I would prefer to handle the response in a new method since that is how all my current code is written.
Thanks
Mitch
You can also use a simple method handler instead of a lambda:
MessageBox.Show("Yes or Now?", modal:false, onclose: this.MessageBox_OnClose); private void MessageBox_OnClose(DialogResult result) { }
Hi Diego,
are you using VS 2013 ?
If yes, this might help you: http://alextselevich.com/2015/08/using-c-6-0-in-visual-studio-2012-and-2013/
Best regards
Frank
Hi Luca,
In “Blocking Modal with callback” code, I’m getting “cannot convert lambda expression to type string because it is not a delegate type” at compile time.
Thanks.
Hi Mitch,
You can use the MessageBox (or any other modal dialog) in several ways: blocking modal and non-blocking modal, with callback and any combination. The blocking modal is the most common and simpler since the code stops waiting for the result:
Blocking Modal
var result = MessageBox.Show("Yes or Now?"); // code here is executed after the message box is closed.
Non-Blocking Modal
MessageBox.Show("Yes or Now?", modal:false); // code here is executed right away.
Blocking Modal with callback
var result = MessageBox.Show("Yes or Now?", (r) => { // code here is execute when the box is closed, r = DialogResult. }); // code here is executed after the message box is closed.
Non-Blocking Modal with Callback
MessageBox.Show("Yes or Now?", modal:false, onclose: this.MessageBox_OnClose); // code here is executed right away. private void MessageBox_OnClose(DialogResult result) { // code here is executed after the message box is closed. };
HTH
Best,
Luca
Please login first to submit.