cancel MouseDown event in WinForms app

柔情痞子 提交于 2019-12-25 04:14:44

问题


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

回答1:


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
}


来源:https://stackoverflow.com/questions/15763343/cancel-mousedown-event-in-winforms-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!