0

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Marcos
  • 40
  • 1
  • 6
  • 1
    what error you are getting ? – Chandan Kumar Dec 19 '13 at 20:35
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 22 '13 at 13:44
  • How do you release a button "out of your control"? Forced bathroom break? If you want to be sure that the mouse is released at the same position it was pressed then use the MouseClick event instead. – Hans Passant Dec 22 '13 at 14:38

2 Answers2

1

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
    }
}
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • You probably guessed correctly (Lord knows). But setting Capture to false when he *subscribes* the event is extremely unlikely to do anything. It should be set to false in the MouseDown event handler. – Hans Passant Dec 22 '13 at 14:35
  • @HansPassant you're right, in a fictional example it works but when there are more controls on form...God knows – Adriano Repetti Dec 22 '13 at 16:24
  • Thanks @Adriano. I use your second exemple and works! – Marcos Dec 22 '13 at 21:58
0

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
    }
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
  • 2
    I assume it's because it's sort-of implied the original poster is already subscribed to the event (despite the really poor code sample). I think he's saying the event fires even though he lets up on the mouse *outside* the bounds of the control. I think that's his problem. I don't think OP's sloppy post merits a downvote, though ;) – sab669 Dec 19 '13 at 21:49
  • Yes, but I am doing this! But in my case I instance the Control with: CreateControlsInstance(); and after I associate the event: MouseUp.... in my object – Marcos Dec 20 '13 at 01:22
  • @Anderson yes, IMO your problem it has captured mouse. Just follow my answer (add this.Capture = false) and it (should) work. – Adriano Repetti Dec 20 '13 at 08:23