|
-
Jan 17th, 2007, 03:06 PM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] mouseHover change image for all buttons
i want to be able to switch to image-2.jpg when mouse is over a button and switch back to image-1.jpg is not over a button... i use mousehover and mouseleave for this... but i want to know how to automate this for all buttons on my form???
-
Jan 17th, 2007, 04:42 PM
#2
New Member
Re: [2005] mouseHover change image for all buttons
try to use
Code:
for each pcb as picture in pnlImages
...then your mouseover code
pnlImages would be the panel where you put all the images on
-
Jan 17th, 2007, 04:58 PM
#3
Thread Starter
Addicted Member
Re: [2005] mouseHover change image for all buttons
-
Jan 17th, 2007, 05:07 PM
#4
New Member
Re: [2005] mouseHover change image for all buttons
i think i may have mis said something,
i'm testing the code right now
-
Jan 17th, 2007, 05:35 PM
#5
New Member
Re: [2005] mouseHover change image for all buttons
so far i have
VB Code:
Public Class Form1
Private sTemp As String = ""
#Region "Hover"
Private Sub pcbImage1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage1.MouseHover
hoversub(sender)
End Sub
Private Sub pcbImage2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage2.MouseHover
hoversub(sender)
End Sub
#End Region
#Region "leave"
Private Sub pcbImage1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage1.MouseLeave
leavesub(sender)
End Sub
Private Sub pcbImage2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage2.MouseLeave
leavesub(sender)
End Sub
#End Region
Private Sub hoversub(ByVal pic As PictureBox)
===> here you should save your image location to stemp
pic.Image = zever.My.Resources.Resources.logo2006 'this is the hover image
End Sub
Private Sub leavesub(ByVal pic As PictureBox)
===> here stemp would come back
End Sub
End Class
now i am using stemp as a temporary storage for the location to the original image (before the hover)
but i don't know the right command for savind the image location or image itself into a string, so he could load it when you hover-out (leave)
-
Jan 17th, 2007, 05:41 PM
#6
Thread Starter
Addicted Member
Re: [2005] mouseHover change image for all buttons
thanks it's quite repetitive.... was wondering whether there's a quicker way and less repetitive method
-
Jan 17th, 2007, 05:52 PM
#7
Re: [2005] mouseHover change image for all buttons
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
AddHandler ctrl.MouseEnter, AddressOf ButtonsMouseEnter
AddHandler ctrl.MouseLeave, AddressOf ButtonsMouseLeave
End If
Next
End Sub
Private Sub ButtonsMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
End Sub
Private Sub ButtonsMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
End Sub
Edit: Of course, this code will not apply for buttons that is in some other container than the form (panels etc.)
-
Jan 17th, 2007, 05:56 PM
#8
Fanatic Member
Re: [2005] mouseHover change image for all buttons
You can have one Sub handle multiple buttons
VB Code:
Private Sub pcbImage_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage1.MouseLeave, pcbImage2.MouseLeave, pcbImage3.MouseLeave.... to however many controls you would like
if you assign each pcbImage.tag a value, then you can check sender.tag so that you know which button (or whatever kind of control you want) raised that event.
I have never done this, but I just checked it out, and you can assign values to the tags of controls and then retreive them from the sender. I hope that made sense.
-
Jan 17th, 2007, 06:00 PM
#9
Re: [2005] mouseHover change image for all buttons
 Originally Posted by 18experience
You can have one Sub handle multiple buttons
VB Code:
Private Sub pcbImage_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage1.MouseLeave, pcbImage2.MouseLeave, pcbImage3.MouseLeave.... to however many you would like
if you assign each pcbImage.tag a value, then you can check sender.tag so that you know which button (or whatever kind of control you want) raised that event.
I have never done this, but I just checked it out, and you can assign values to the tags of controls and then retreive them from the sender. I hope that made sense.
Thats what my code does, it dynamically adds an event handler for each buttons MouseEnter/MouseLeave event. You dont even need to check the tag, you can just compare sender to a button like so:
VB Code:
If Sender Is Button1 Then
'Yeah!
End if
Or you can directly use the sender object:
VB Code:
Dim clickedButton as Button = DirectCast(sender, button)
clickedButton.Visible = False
-
Jan 18th, 2007, 02:43 AM
#10
Thread Starter
Addicted Member
Re: [2005] mouseHover change image for all buttons
 Originally Posted by Atheist
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
AddHandler ctrl.MouseEnter, AddressOf ButtonsMouseEnter
AddHandler ctrl.MouseLeave, AddressOf ButtonsMouseLeave
End If
Next
End Sub
Private Sub ButtonsMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
End Sub
Private Sub ButtonsMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
End Sub
Edit: Of course, this code will not apply for buttons that is in some other container than the form (panels etc.)
works well!!! that's the easiest way i think thank u!!!
-
Jan 18th, 2007, 02:52 AM
#11
Re: [RESOLVED] [2005] mouseHover change image for all buttons
What you should be doing is creating your own control that inherits the Button class. You would add an extra property like HoverImage, and then override the OnMouseHover and OnMouseLeave methods of the class. You then add instances of this control to your forms, set the HoverImage property and the rest is taken care of by the class itself. VB.NET is an OO language and you should take advantage of that.
-
Jan 18th, 2007, 04:50 AM
#12
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] mouseHover change image for all buttons
yeah i wanted to do something like this but i must always create the button object dynamically?? i mean can i use it in "design mode"...that place it graphically on the form like the other buttons??? if yes how?
-
Jan 18th, 2007, 07:38 AM
#13
Re: [RESOLVED] [2005] mouseHover change image for all buttons
 Originally Posted by lplover2k
yeah i wanted to do something like this but i must always create the button object dynamically?? i mean can i use it in "design mode"...that place it graphically on the form like the other buttons??? if yes how?
Create your inherited class, build the project and it will be added to the Toolbox automatically for that project and any projects in the same solution that reference it. If you want to use it outside the solution in which it's declared then you can add it to the Toolbox manually, just as you can any component.
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
|