Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

how can i change the image of a picturebox control at runtime when mouse hovers it??

heres my code

 

Private Sub hoverDiente(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles d1.MouseHover, d2.MouseHover
       sender = CType(sender, PictureBox)
       Dim tempStr As String = sender.name
       tempStr.Replace("d", "")
       MsgBox(Me.path)
       sender.Image = New Bitmap(Me.path + "\\path\\" + tempStr + ".jpg")

   End Sub

 

so far i have an 'invalid argument' error when calling sender.image=new bitmap()

 

thanks for your help

  • Leaders
Posted

I know that this is not what you were asking and I'm not trying to rag on your coding, but I just want to point this out, in case you aren't aware. Note the underlined code

 

Private Sub hoverDiente(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles d1.MouseHover, d2.MouseHover
       [u][b]sender = CType(sender, PictureBox)[/b][/u]
       Dim tempStr As String = sender.name
       tempStr.Replace("d", "")
       MsgBox(Me.path)
       sender.Image = New Bitmap(Me.path + "\\path\\" + tempStr + ".jpg")

   End Sub

This line casts "sender" to type PictureBox, and stores the reference back into the "sender" variable, whose type is Object, essentially uncasting to object and forcing you to use late binding when you access sender.Image. This would be a better approach.

'At the top of the file
Option Strict On

'Hover image code
Private Sub hoverDiente(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles d1.MouseHover, d2.MouseHover
       [u][b]Dim picSender As PictureBox = CType(sender, PictureBox)[/b][/u]
       Dim tempStr As String = picSender.name
       tempStr.Replace("d", "")
       MsgBox(Me.path)
       picSender.Image = New Bitmap(Me.path + "\\path\\" + tempStr + ".jpg")

   End Sub

 

Another note, this one related to the issue at hand: when constructing file names, I recommend using the IO.Path class to avoid issues with missing/doubled backslashes.

 

'At the top of the file
Option Strict On

'Hover image code
Private Sub hoverDiente(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles d1.MouseHover, d2.MouseHover
       Dim picSender As PictureBox = CType(sender, PictureBox)
       Dim tempStr As String = picSender.name
       tempStr.Replace("d", "")
       MsgBox(Me.path)
       [b][u]picSender.Image = New Bitmap(IO.Path.Combine(_ 
           Me.path & "\\path\\" & tempStr & ".jpg")[/u][/b]

   End Sub

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...