change cursor over a pictureBox
Asked Answered
P

3

6

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.

Prehension answered 22/9, 2013 at 22:6 Comment(0)
S
16

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;
    }
Strychnic answered 23/9, 2013 at 8:26 Comment(0)
P
1

seem like i should have known better how to use Cursors class .

cursor=Cursors.hand;

rather than

cursor.current=Cursors.hand;

that was embarrassing ..

Prehension answered 22/9, 2013 at 22:26 Comment(0)
A
1

only add MouseMove event on pictureBox and set a Cursor for this

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Cursor = Cursors.Hand;
        }
Ation answered 22/1, 2017 at 16:5 Comment(1)
Please provide some explanation for you code to be easier understood by the rest.Flightless

© 2022 - 2024 — McMap. All rights reserved.