-
Mar 31st, 2019, 02:38 AM
#1
Thread Starter
Member
[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?
-
Mar 31st, 2019, 02:55 AM
#2
Re: Picturebox MouseEnter/MouseLeave events
MouseEnter only occurs when your mouse first enters a control. Use the MouseMove event instead
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 31st, 2019, 03:04 AM
#3
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 31st, 2019, 03:29 AM
#4
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".
-
Mar 31st, 2019, 03:55 AM
#5
Thread Starter
Member
Re: Picturebox MouseEnter/MouseLeave events
Thanks for the fast replies!
Originally Posted by passel
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.
-
Mar 31st, 2019, 04:19 AM
#6
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.
-
Mar 31st, 2019, 04:34 AM
#7
Thread Starter
Member
Re: Picturebox MouseEnter/MouseLeave events
Originally Posted by passel
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.
-
Mar 31st, 2019, 04:47 AM
#8
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.
-
Mar 31st, 2019, 04:49 AM
#9
Thread Starter
Member
Re: Picturebox MouseEnter/MouseLeave events
Originally Posted by passel
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!
-
Mar 31st, 2019, 10:46 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|