AlexSavAlexandrov
AlexSavAlexandrov

Reputation: 873

How can I make an imitation of a button control with a picture box, that behaves exactly like a button?

Buttons do that: if the user preses and holds he left mouse button over a button control and moves the cursor out of the control, the button control changes it's appearance back to default, and if the mouse button is still being held, and the cursor enters a button control, the control changes it's appearance to it's pressed version. I'm trying to imitate a button using a PictureBox, but when the mouse leaves the PictureBox before the left mouse button is released, the PictureBox's picture doesn't change until the mouse button is released.

I'm trying to do this because the button control can't look the way I want to.

How can I make an imitation of a button control with a picture box, that behaves exactly like a button?

Upvotes: 1

Views: 1049

Answers (2)

Hans Passant
Hans Passant

Reputation: 942538

This is by design, the control sets the Capture property to true so it will keep receiving mouse messages while the button is held down when you move the mouse outside of the control rectangle.

You could turn it off when you see it moving out:

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
        var box = (PictureBox)sender;
        if (!box.DisplayRectangle.Contains(e.Location)) box.Capture = false;
    }

Upvotes: 1

Charlie
Charlie

Reputation: 9153

Try using the DragLeave event as opposed to the MouseLeave event.

Upvotes: 0

Related Questions