简体   繁体   中英

MouseUp Event Error

I am new to WinForms events and I am getting a strange error.Well I write when I start my control:

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:

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

As alternative just check in MouseUp that mouse is still inside your control:

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()

    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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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