Alex
Alex

Reputation: 5257

break onclick event from method

Button's OnClick event is called:

private void vkb7_Click_1(object sender, EventArgs e)
{
    //method
    button_start(object sender);

    // do stuff 2
}

Some method is triggered:

private void button_start(object sender)
{
    bool some = true;
    if (some)
    {
        return; // I'd also like to break "button click"
    }
}

I'd like to break "button click" event from method - how do I do it?

Upvotes: 1

Views: 4165

Answers (4)

syed mohsin
syed mohsin

Reputation: 2938

First, Set the return type of button_start to bool
Second, If you want to pass sender of vkb7 button or properties of vkb7 button,
Then you should pass it like this:

button_start(sender) So the code would be like this:

 private void vkb7_Click_1(object sender, EventArgs e)
 {
   //method
   if(button_start(sender))
   // do stuff 2
 }

Then in the method

 private bool button_start(object sender)
 {
    bool some = true;
    if (some)
    {
       return true; // I'd also like to break "button click"
    }
    else  
    return false;
 }

Upvotes: 0

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23695

You can't really "block" the event as it has already been fired. You can however handle the click manually depending on the result.

private void vkb7_Click_1(object sender, EventArgs e)
{
    bool result = button_start(sender);

    if (result)
        DoSomething();
    else
        DoSomethingElse();
}

private bool button_start(object sender)
{
    bool some = true;

    if (some)
        return false;

    return true;
}

Upvotes: 0

Mitch
Mitch

Reputation: 22311

If you are determining in button_start whether actions should occur in vkb7_Click_1, consider making button_start return bool and then use the return value in vkb7_Click_1.

On the other hand, if you are encountering an error condition in button_start, throw an exception.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273804

Simple, let button_start() return a boolean:

private bool button_start(object sender)
{
   bool some = ...;
   return some;
}


private void vkb7_Click_1(object sender, EventArgs e)
{
  //method
  if (button_start(object sender))
    return;

  // do stuff 2
}

Upvotes: 4

Related Questions