MessageBox with Event handler

0
0

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 must to post comments
0
0

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) {
}


  • Diego
    • Diego
    • Jan 9, 2017 - 5:12 pm
    Hi Luca, This is the “Non-Blocking Modal with Callback” as per you code snippet above. Just wondering why the code snippet for “Blocking Modal with callback” is not working. Maybe, I am missing something. Thanks.
  • Luca (ITG)
    Just tried, it’s the same, remove modal:false => MessageBox.Show(“Yes or Now?”, onclose: MessageBox_OnClose);
  • Diego
    • Diego
    • Jan 9, 2017 - 8:07 pm
    Hi Luca, Yes, above syntax works. Does it mean the using lambda expression won’t work? Thanks.
  • Luca (ITG)
    Lambda works too, it’s the same thing. MessageBox.Show(“Yes or Now?”, onclose: (result)=>{ … } );
  • Diego
    • Diego
    • Jan 9, 2017 - 8:36 pm
    Hi Luca, It works. Thanks.
  • You must to post comments
0
0

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

  • Diego
    • Diego
    • Jan 9, 2017 - 5:10 pm
    Hi Frank, Currently sing VS 2015 – Update 3. Thanks.
  • You must to post comments
0
0

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.

  • You must to post comments
0
0

Thanks.. Perfect.

  • You must to post comments
0
0

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

 

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.