Make custom button using images

topsykretts

Well-known member
Joined
Dec 22, 2011
Messages
47
Programming Experience
Beginner
Hello there. I am trying to make an custom button which will use images from below for appropriate action.

normal.gifover.gifdown.gif

I tried to write code but there is something mising.
VB.NET:
    Private Sub PictureBox1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseHover
        PictureBox1.Image = My.Resources.normal
    End Sub

   Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        PictureBox1.Image = My.Resources.down
    End Sub

Image for the normal state is imported directly to the image.

Thanks in advance
 
Are you aware that the MouseHover event doesn't mean that the mouse pointer is over the control but rather than the mouse pointer is over the control AND stopped moving. If you want the "button" to look different when the mouse pointer is over it and when it's not then you have to handle both the MouseEnter and MouseLeave events.

Similarly, the MouseClick event occurs when the user depresses and releases a mouse button. What you should be doing is handling the MouseDown and MouseUp events. You might also want to think about what the difference between the right and left or even other mouse buttons means.
 
Are you aware that the MouseHover event doesn't mean that the mouse pointer is over the control but rather than the mouse pointer is over the control AND stopped moving. If you want the "button" to look different when the mouse pointer is over it and when it's not then you have to handle both the MouseEnter and MouseLeave events.

Similarly, the MouseClick event occurs when the user depresses and releases a mouse button. What you should be doing is handling the MouseDown and MouseUp events. You might also want to think about what the difference between the right and left or even other mouse buttons means.



First, Thank you very much jmcilhinney.

Reason why I made mistake in code:
In MouseHover event I tried with proper image "over.gif", but I made mistake because that picture is named "_02_over" in my resources and when I had to adapt code to the forum I renamed to the "normal" instead of "over".

I realize that I missed four more events and my code now looks:
VB.NET:
    Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
        PictureBox1.Image = My.Resources._02_over
    End Sub

    Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        PictureBox1.Image = My.Resources._02
    End Sub

     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As  System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        PictureBox1.Image = My.Resources._02_down
    End Sub

     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As  System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        PictureBox1.Image = My.Resources._02_over
    End Sub
 
Last edited:
Back
Top