Results 1 to 10 of 10

Thread: [RESOLVED] Picturebox MouseEnter/MouseLeave events

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Resolved [RESOLVED] Picturebox MouseEnter/MouseLeave events

    I am using this:

    Code:
    Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
            Dim roptionsl As Object = My.Resources.ResourceManager.GetObject("optionsl.png")
            PictureBox2.BackgroundImage = roptionsl
        End Sub
    
        Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
            Dim roptions As Object = My.Resources.ResourceManager.GetObject("options.png")
            PictureBox2.BackgroundImage = roptions
        End Sub
    and when I hover the image disappears. Am I missing something?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Picturebox MouseEnter/MouseLeave events

    MouseEnter only occurs when your mouse first enters a control. Use the MouseMove event instead

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    Re: Picturebox MouseEnter/MouseLeave events

    Ideally you should be casting your Objects to Bitmap, which is the correct type...

    Code:
    PictureBox2.BackgroundImage = DirectCast(roptionsl, BitMap)

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picturebox MouseEnter/MouseLeave events

    I'm not sure why they should switch to the MouseMove event.
    I'm also not sure why the image would disappear when the mouse hovers.
    It looks like the intent is to "highlight" the picturebox when the mouse enters the picturebox, and remove the highlight when it exits, so the code seems like it should work, essentially.

    I just did a quick test and it worked as I would expect.
    Code:
      Private bitmap1 As New Bitmap("c:\c\man1.png")
      Private bitmap2 As New Bitmap("c:\c\man0.png")
    
      Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
        PictureBox2.BackgroundImage = bitmap2
      End Sub
    
      Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
        PictureBox2.BackgroundImage = bitmap1
      End Sub
    The background image changed when I entered the picturebox and remained even while the mouse hovered. It didn't change again until I left the picturebox. I don't think pulling the image from Resources should be the issue, although I didn't replicate that in my test. I wonder if there is also code in the Hover event which might be drawing something, or something in the Paint event, which could be drawing over the background image making it "disappear".

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Picturebox MouseEnter/MouseLeave events

    Thanks for the fast replies!

    Quote Originally Posted by passel View Post
    I'm not sure why they should switch to the MouseMove event.
    I'm also not sure why the image would disappear when the mouse hovers.
    It looks like the intent is to "highlight" the picturebox when the mouse enters the picturebox, and remove the highlight when it exits, so the code seems like it should work, essentially.

    I just did a quick test and it worked as I would expect.
    Code:
      Private bitmap1 As New Bitmap("c:\c\man1.png")
      Private bitmap2 As New Bitmap("c:\c\man0.png")
    
      Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
        PictureBox2.BackgroundImage = bitmap2
      End Sub
    
      Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
        PictureBox2.BackgroundImage = bitmap1
      End Sub
    The background image changed when I entered the picturebox and remained even while the mouse hovered. It didn't change again until I left the picturebox. I don't think pulling the image from Resources should be the issue, although I didn't replicate that in my test. I wonder if there is also code in the Hover event which might be drawing something, or something in the Paint event, which could be drawing over the background image making it "disappear".
    I tried:

    Code:
        Private roptionsl As New Bitmap(My.Resources.ResourceManager.GetString("optionsl.png"))
        Private roptions As New Bitmap(My.Resources.ResourceManager.GetString("options.png"))
    
        Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
            'Dim roptionsl As Object = My.Resources.ResourceManager.GetObject("optionsl.png")
            PictureBox2.BackgroundImage = roptionsl
            'PictureBox2.BackgroundImage = DirectCast(roptionsl, Bitmap)
        End Sub
    
        Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
            'Dim roptions As Object = My.Resources.ResourceManager.GetObject("options.png")
            PictureBox2.BackgroundImage = roptions
            'PictureBox2.BackgroundImage = DirectCast(roptions, Bitmap)
        End Sub
    and I get the error System.ArgumentNullException: 'Value cannot be null.' on
    Code:
    Private roptionsl As New Bitmap(My.Resources.ResourceManager.GetString("optionsl.png"))
    Must be something with bitmap parameter filename.

    @.paul. when I tried PictureBox2.BackgroundImage = DirectCast(roptions, Bitmap) I did not get any error, but the image disappeared when I hovered it. Did not use event MouseMove.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picturebox MouseEnter/MouseLeave events

    I'm not sure why you using Resources.ResourceManager to get the objects. And switching to GetString doesn't make any sense.

    I just added the two files I had in my earlier example to the Resources, i.e. went to the Resources tab and selected "Add Resource > Add existing file..." from the menu and selected the two png files.

    Then in the code, I just reference those two resources.
    Code:
    Public Class Form1
        Private bitmap1 As Bitmap = My.Resources.man0
        Private bitmap2 As Bitmap = My.Resources.man1
    
        Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
            PictureBox2.BackgroundImage = bitmap2
        End Sub
    
        Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
            PictureBox2.BackgroundImage = bitmap1
        End Sub
    End Class
    As I mentioned earlier, if the image disappears when you hover, then you must have something else going on in your code.
    Just to validate a concept, when something doesn't work as expected, you might want try out the concept in a new project without all the extra baggage you might have in your current project. If it works in isolation, then there is something else in your "real" project that is causing an issue, outside the code you're using to implement your concept.
    Last edited by passel; Mar 31st, 2019 at 04:25 AM.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Picturebox MouseEnter/MouseLeave events

    Quote Originally Posted by passel View Post
    I'm not sure why you using Resources.ResourceManager to get the objects. And switching to GetString doesn't make any sense.

    I just added the two files I had in my earlier example to the Resources, i.e. went to the Resources tab and selected "Add Resource > Add existing file..." from the menu and selected the two png files.

    Then in the code, I just reference those two resources.
    Code:
    Public Class Form1
        Private bitmap1 As Bitmap = My.Resources.man0
        Private bitmap2 As Bitmap = My.Resources.man1
    
        Private Sub PictureBox2_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox2.MouseEnter
            PictureBox2.BackgroundImage = bitmap2
        End Sub
    
        Private Sub PictureBox2_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox2.MouseLeave
            PictureBox2.BackgroundImage = bitmap1
        End Sub
    End Class
    As I mentioned earlier, if the image disappears when you hover, then you must have something else going on in your code.
    Just to validate a concept, when something doesn't work as expected, you might want try out the concept in a new project without all the extra baggage you might have in your current project. If it works in isolation, then there is something else in your "real" project that is causing an issue, outside the code you're using to implement your concept.
    I didn't know how to not use resourcemanager, thanks. The image still disappears, I'm guessing that it has something to do with BackgroundImageLayout options, I need to have it to "Stretch", because the image is too big. In any case, I will mark this thread as resolved. Hope I work it out.

    UPDATE: Yes PictureBox2.BackgroundImageLayout = ImageLayout.Stretch did the work, thanks!
    Last edited by Pixy; Mar 31st, 2019 at 04:47 AM.

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picturebox MouseEnter/MouseLeave events

    Ok. I would still try a new project with nothing else in it other than the one test.
    I don't see how having the layout set to stretch would make a difference.
    I did make my picturebox smaller, and set the layout to stretch and it still worked as I would expect. Hovering does not cause the image to disappear.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2018
    Posts
    39

    Re: Picturebox MouseEnter/MouseLeave events

    Quote Originally Posted by passel View Post
    Ok. I would still try a new project with nothing else in it other than the one test.
    I don't see how having the layout set to stretch would make a difference.
    I did make my picturebox smaller, and set the layout to stretch and it still worked as I would expect. Hovering does not cause the image to disappear.
    When I put BackgroundImageLayout to none the image doesn't even show up in the first place. It is being misplaced. PictureBox2.BackgroundImageLayout = ImageLayout.Stretch did the work!

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picturebox MouseEnter/MouseLeave events

    That just makes it sound like the picture is large enough that when you set the ImageLayout to none (or the default Tile), then all you're seeing is the upper left corner of the image which looks like it didn't show up because it must be a solid color, or could be transparent.
    If the ImageLayout is none, or Tile, the image should be its full size, with the upper left corner located at the upper left corner of the client area of the picturebox.

    Of course, that only explains what you're seeing in post #9, not what you've reported seeing in posts # 1 to 7.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width