Search code examples
c#winformsmousedown

cancel MouseDown event in WinForms app


I've several count variables with min and max values. While I press the appropriate buttons the counter goes up or down.

While pressing the lmb and the counter exceeds e.g. 100 (max value) it doesnt stop counting naturally.

Is there a way to check my counter and force the end of the mouse event without watching over it manually?

private void button_RunXPositive_MouseDown(object sender, MouseEventArgs e)
{
    if (X < 100) {
      StartInMode(0, true); // a motor 
      ((Button_Triangle)sender).BackColor = ((Button_Triangle)sender).EdgeColor;
      return;
    }  
    else {
      ((Button_Triangle)sender).BackColor = Color.Black;
      this.MotorStop((UInt32)0);
    }
}

Solution

  • If your project is an WPF project then you can write this in your event handler:

    if(counter < min || counter > max)
    {
        e.Handled = true;
    }
    //your code
    

    EDIT If you're using Windows Form so you can do this:

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        if(counter < min || counter > max)
        {
            return;
        }
    
        //your code
    }