change cursor over a pictureBox

LoneXcoder picture LoneXcoder · Sep 23, 2013 · Viewed 19.7k times · Source

i was trying to implemet an image button in winforms application as i can ...easy when using asp.net the problem seem to be(i suspect) that when the mouse is over the image inside the picturebox it is not responding or not triggering the mouseEnter event

it looks like if i had a picture that is smaller than the pictureBox Size it will accept the reason to trigger the event but over the image within the pictureBox it would Not ?

the trick was to set pictureBox to sizeMode=zoom. then do 2 things when the mouse is over the "imageButton" : change the size of PictureBox a little larger + change cursor to hand

so i will get a kind of mouse over effect as i could with asp.net

did anyone have that problem ? at first i tried mouseHover, then i thought enter would do better as it only requiers the mouse to pass the borders of the picture box... both enter and hover events did not work for me ...

Edit :

the event does trigger , i can see that if i initially set sizemode to CenterImage and inside the event i ask for sizemode=zoom, so the effect dose occur ..but cursor.current=Cursors.Hand will not change.

Answer

user2509901 picture user2509901 · Sep 23, 2013

This should work

    private void pictureBox1_MouseEnter(object sender, EventArgs e)
    {
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Cursor = Cursors.Hand;
    }

    private void pictureBox1_MouseLeave(object sender, EventArgs e)
    {
        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        pictureBox1.Cursor = Cursors.Default;
    }