VorTex.Zerg
VorTex.Zerg

Reputation: 73

Stop a function in the form with 'Cancel" button in the messagBox

I put s messageBox in my Button Event that when user click on the button that MessageBox will show to user and it has Okay and Cancel Buttons in the MessageBox. Now when user click on Okay and Cancel button it will done a same function. But i wanna when user click on the cancel Button Stop that function and back to the form. Just imagine when user click on the Okay button it will open a new WebBrowser which direct to DropBox Site for download something. But when user click on the cancel button it will do same thing. But i don't want. I just searched and find a code which include this but when i put in my program nothing happened and again cancel button will open a new WebBrowser. You can see my code in the Code sample below :

        private void btnWCS2016_Click(object sender, EventArgs e)
    {
        DialogResult dr =  MessageBox.Show("Hint :\nRaR password : vortex\n\n Winner : Polt (Terran)\n\nDownload require : 57.166 MB","User advice",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
        System.Diagnostics.Process.Start("https://www.dropbox.com/home?preview=WCS-+Winter+2016.rar");
        if(dr==DialogResult.Cancel)
        {
            return;
        }
    }

Upvotes: 0

Views: 312

Answers (2)

VVN
VVN

Reputation: 1610

You can Use Kill() method to stop the process.

private void btnWCS2016_Click(object sender, EventArgs e)
    {
       DialogResult dr =  MessageBox.Show("Hint :\nRaR password : vortex\n\n Winner : Polt (Terran)\n\nDownload require : 57.166 MB","User advice",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);

       if(dr==DialogResult.Cancel)
       {
           return; 
       }

      System.Diagnostics.Process.Start("https://www.dropbox.com/home?preview=WCS-+Winter+2016.rar");


    }

Upvotes: 1

Zohar Peled
Zohar Peled

Reputation: 82474

You start the process before checking the returned value from the message box. Either move the process start after the condition, like this:

private void btnWCS2016_Click(object sender, EventArgs e)
{
    DialogResult dr =  MessageBox.Show("Hint :\nRaR password : vortex\n\n Winner : Polt (Terran)\n\nDownload require : 57.166 MB","User advice",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);

    if(dr==DialogResult.Cancel)
    {
        return;
    }
    System.Diagnostics.Process.Start("https://www.dropbox.com/home?preview=WCS-+Winter+2016.rar");
}

or change the condition and put it inside, like this:

private void btnWCS2016_Click(object sender, EventArgs e)
{
    DialogResult dr =  MessageBox.Show("Hint :\nRaR password : vortex\n\n Winner : Polt (Terran)\n\nDownload require : 57.166 MB","User advice",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);

    if(dr==DialogResult.OK)
    {
        System.Diagnostics.Process.Start("https://www.dropbox.com/home?preview=WCS-+Winter+2016.rar");

    }
}

Personally I prefer the second way in this case, since it's only one row code. if it was a massive code block i would prefer the first way.

Upvotes: 3

Related Questions