-4

I'm going to create a scenario before I ask my question. Say you have a form with a start button, then when the user presses it a message box pops up asking if they want to continue and the options are yes or no. When the user presses no the program should stop what its doing, return to the form and clear the text boxes/combo boxes/etc.

Now here's my question, upon selecting "no" how does one go about stopping the program from going into further code? For the code to stop what it's doing and return to the form like nothing happened?

I hope I explained myself well enough...

Nevermind everyone I realised where I went wrong... Crisis averted. Thanks for all of your input however!

user2619395
  • 249
  • 1
  • 6
  • 15
  • 1
    A concrete example of what you are trying to `stop` will help a lot – Steve Aug 13 '13 at 18:47
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Aug 13 '13 at 18:47
  • have you written any code? we need to see what you have tried! – phillip Aug 13 '13 at 18:47
  • if user selects no, Just return; – Kurubaran Aug 13 '13 at 18:47
  • 1
    MessageBox with ok/cancel buttons, check the MessageBoxResult of the form, then either process or clear out your controls. Trivial when done correctly, annoying when not, disastrous when you have no clue. –  Aug 13 '13 at 18:47
  • You're probably [looking for this](http://stackoverflow.com/questions/3036829/messagebox-with-yes-no-and-dialogresult) – Shaz Aug 13 '13 at 18:48
  • You need to shore up your question with actual code. Many confirm style modal windows return a status that indicates what got pressed. We can't guess what you're actually doing. You don't even mention if it's wpf or winforms. – spender Aug 13 '13 at 18:48
  • `return;` will handle what you are looking for unless you are looking to do some other stuff.. then assign a boolean and check what it's return value is for additional post `NO`` processing – MethodMan Aug 13 '13 at 18:48

2 Answers2

2

Normally you'll use a Modal form, means that your current method is blocked and will be blocked until the modal form returns.

You can try something like this:

        DialogResult result = MessageBox.Show("Title", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

        if (result == DialogResult.No)
            return;

If you want a custom form to show as modal, you should use ShowDialog() instead of Show(), ShowDialog will block the current execution of the executing method and will setup a temporary messageloop for handling window messages.

like:

using(FormQuestion form = new FormQuestion())
{
    DialogResult result = form.ShowDialog();
    // check the result.
}

Check here for more information: Displaying Modal and Modeless Windows Forms http://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

In general, .NET uses a cooperative cancellation model, so methods need to have a mechanism to notify that a cancellation is requested, and they need to explicitly cancel themselves.

This is handled by many portions of the framework via the CancellationTokenSource class and the CancellationToken struct.

For full details, see Cancellation in Managed Threads on MSDN.

That being said, if the method in question is using the MessageBox directly, it can just return or stop working if the result is DialogResult.No (in Windows Forms) or Window.DialogResult is unset or false (in WPF).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373