[英]MouseUp Event Error
我是WinForms事件的新手,但遇到一个奇怪的错误,在启动控件时写了一下:
this.MouseUp += MouseUpMethod;
但是问题是,当我从控件中释放鼠标按钮时,该程序会识别为在控件上释放鼠标。 我无法理解此错误。 有没有人得到这个错误?
这是因为,默认情况下,您的控件会捕获鼠标。 只需在MouseDown
事件处理程序的
某处
将Control.Capture设置为false
,例如:
void MouseDown(object sender, MouseEventArgs e) {
this.Capture = false;
}
或者,只需在MouseUp
中检查鼠标仍在控件内:
void MouseUp(object sender, MouseEventArgs e) {
if (ClientRectangle.Contains(PointToClient(Cursor.Position))) {
// Your code here
}
}
看到,您需要在InitializeComponent()之后将事件与事件处理程序关联
public Form1()
{
InitializeComponent();
this.button2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button2_MouseUp);
}
那么您的事件处理程序应该是
private void button2_MouseUp(object sender, MouseEventArgs e)
{
//Do stuff here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.