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???
Printable View
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???
try to use
pnlImages would be the panel where you put all the images onCode:for each pcb as picture in pnlImages
...then your mouseover code
where do i put this??
i think i may have mis said something,
i'm testing the code right now
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)
thanks it's quite repetitive.... was wondering whether there's a quicker way and less repetitive method
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.)
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.
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:Quote:
Originally Posted by 18experience
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
works well!!! that's the easiest way i think thank u!!!Quote:
Originally Posted by Atheist
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.
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.Quote:
Originally Posted by lplover2k