简体   繁体   English

MouseUp事件错误

[英]MouseUp Event Error

I am new to WinForms events and I am getting a strange error.Well I write when I start my control: 我是WinForms事件的新手,但遇到一个奇怪的错误,在启动控件时写了一下:

this.MouseUp += MouseUpMethod;

But the problem is, when I release the mouse button out of my control, the program recognize as I release the mouse over the Control. 但是问题是,当我从控件中释放鼠标按钮时,该程序会识别为在控件上释放鼠标。 I am not able to understand this error. 我无法理解此错误。 Did ever someone got this error? 有没有人得到这个错误?

It's because, by default, your control captures mouse. 这是因为,默认情况下,您的控件会捕获鼠标。 Just set Control.Capture to false somewhere in your MouseDown event handler, for example: 只需在MouseDown事件处理程序的 某处 Control.Capture设置为false ,例如:

void MouseDown(object sender, MouseEventArgs e) {
    this.Capture = false;
}

As alternative just check in MouseUp that mouse is still inside your control: 或者,只需在MouseUp中检查鼠标仍在控件内:

void MouseUp(object sender, MouseEventArgs e) {
    if (ClientRectangle.Contains(PointToClient(Cursor.Position))) {
        // Your code here
    }
}

see, you need to associate event with event handler just after your InitializeComponent() 看到,您需要在InitializeComponent()之后将事件与事件处理程序关联

    public Form1()
    {
        InitializeComponent();
        this.button2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button2_MouseUp);
    }

then your event handler should be 那么您的事件处理程序应该是

    private void button2_MouseUp(object sender, MouseEventArgs e)
    {
        //Do stuff here
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM