ramone Posted September 30, 2005 Posted September 30, 2005 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 Quote
sgt_pinky Posted September 30, 2005 Posted September 30, 2005 Your method is correct. The path to the image must be wrong. Quote
Administrators PlausiblyDamp Posted September 30, 2005 Administrators Posted September 30, 2005 What is Me.path in your code? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted September 30, 2005 Leaders Posted September 30, 2005 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 Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.