Results 1 to 13 of 13

Thread: [RESOLVED] [2005] mouseHover change image for all buttons

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Resolved [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???

  2. #2
    New Member
    Join Date
    Jan 2007
    Posts
    3

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: [2005] mouseHover change image for all buttons

    where do i put this??

  4. #4
    New Member
    Join Date
    Jan 2007
    Posts
    3

    Re: [2005] mouseHover change image for all buttons

    i think i may have mis said something,
    i'm testing the code right now

  5. #5
    New Member
    Join Date
    Jan 2007
    Posts
    3

    Re: [2005] mouseHover change image for all buttons

    so far i have

    VB Code:
    1. Public Class Form1
    2.  
    3.     Private sTemp As String = ""
    4. #Region "Hover"
    5.     Private Sub pcbImage1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage1.MouseHover
    6.         hoversub(sender)
    7.     End Sub
    8.  
    9.     Private Sub pcbImage2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage2.MouseHover
    10.         hoversub(sender)
    11.     End Sub
    12. #End Region
    13.  
    14. #Region "leave"
    15.     Private Sub pcbImage1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage1.MouseLeave
    16.         leavesub(sender)
    17.     End Sub
    18.  
    19.     Private Sub pcbImage2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles pcbImage2.MouseLeave
    20.         leavesub(sender)
    21.     End Sub
    22. #End Region
    23.  
    24.     Private Sub hoversub(ByVal pic As PictureBox)
    25. ===> here you should save your image location to stemp
    26.         pic.Image = zever.My.Resources.Resources.logo2006 'this is the hover image
    27.  
    28.     End Sub
    29.  
    30.     Private Sub leavesub(ByVal pic As PictureBox)
    31. ===> here stemp would come back
    32.     End Sub
    33. 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)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] mouseHover change image for all buttons

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         For Each ctrl As Control In Me.Controls
    3.             If TypeOf ctrl Is Button Then
    4.                 AddHandler ctrl.MouseEnter, AddressOf ButtonsMouseEnter
    5.                 AddHandler ctrl.MouseLeave, AddressOf ButtonsMouseLeave
    6.             End If
    7.         Next
    8.     End Sub
    9.  
    10.  
    11.     Private Sub ButtonsMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
    12.  
    13.     End Sub
    14.  
    15.  
    16.     Private Sub ButtonsMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
    17.  
    18.     End Sub

    Edit: Of course, this code will not apply for buttons that is in some other container than the form (panels etc.)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    Re: [2005] mouseHover change image for all buttons

    You can have one Sub handle multiple buttons
    VB Code:
    1. 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.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] mouseHover change image for all buttons

    Quote Originally Posted by 18experience
    You can have one Sub handle multiple buttons
    VB Code:
    1. 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:
    1. If Sender Is Button1 Then
    2.     'Yeah!
    3. End if

    Or you can directly use the sender object:

    VB Code:
    1. Dim clickedButton as Button = DirectCast(sender, button)
    2. clickedButton.Visible = False
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    Re: [2005] mouseHover change image for all buttons

    Quote Originally Posted by Atheist
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         For Each ctrl As Control In Me.Controls
    3.             If TypeOf ctrl Is Button Then
    4.                 AddHandler ctrl.MouseEnter, AddressOf ButtonsMouseEnter
    5.                 AddHandler ctrl.MouseLeave, AddressOf ButtonsMouseLeave
    6.             End If
    7.         Next
    8.     End Sub
    9.  
    10.  
    11.     Private Sub ButtonsMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
    12.  
    13.     End Sub
    14.  
    15.  
    16.     Private Sub ButtonsMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseEnter
    17.  
    18.     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!!!

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    128

    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?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] mouseHover change image for all buttons

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width