Results 1 to 19 of 19

Thread: How do I make colour changes on mouse movement?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Question How do I make colour changes on mouse movement?

    I have got four option boxes on my form and when i put my mouse over the option boxes I want then to change colour.

    What I have at the moment is,

    VB Code:
    1. Private Sub opt1_MouseMove()
    2.  
    3.     Let opt1.BackColor = vbYellow
    4.     Let opt2.BackColor = vbGreen
    5.     Let opt3.BackColor = vbGreen
    6.     Let opt4.BackColor = vbGreen
    7.    
    8. End Sub

    Could someone please help?

    EDIT

    I'm using VBA and i would like the colour of the option boxes to change when they are selected or clicked on
    Last edited by Shmokin; Feb 15th, 2006 at 02:32 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I make colour changes on mouse movement?

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    4. Private Declare Function ReleaseCapture Lib "user32" () As Long
    5. Private Declare Function GetCapture Lib "user32" () As Long
    6.  
    7. Private Sub Option1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    8. If (X < 0) Or (Y < 0) Or (X > Option1.Width) Or (Y > Option1.Height) Then
    9.        ReleaseCapture
    10.        Option1.BackColor = &H8000000F
    11. ElseIf GetCapture() <> Option1.hwnd Then
    12.        SetCapture Option1.hwnd
    13.        Option1.BackColor = vbGreen
    14. End If
    15. End Sub

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How do I make colour changes on mouse movement?

    Do you mean that you want to change the color when the mouse hover one of the Option buttons and restore it when the mouse moves away? Even though it's easy enough to determent when the mouse is over a control (because of the MouseMove event) VB unfortunatly doesn't expose a MouseLeave event. The usual hack to accomplish this is to use the SetCapture API function.
    VB Code:
    1. Private Declare Function SetCapture Lib "user32.dll" ( _
    2.     ByVal hwnd As Long _
    3. ) As Long
    4.  
    5. Private Declare Function ReleaseCapture Lib "user32.dll" () As Long
    6. Private Declare Function GetCapture Lib "user32.dll" () As Long
    7.  
    8. Public Sub SetColor(opt As OptionButton, x As Single, y As Single)
    9.     If GetCapture <> opt.hwnd Then
    10.         Call SetCapture(opt.hwnd)
    11.     End If
    12.     If (x < 0 Or x > opt.Width) Or (y < 0 Or y > opt.Height) Then
    13.         ReleaseCapture
    14.         opt.BackColor = vbGreen '<- Color you want when the mouse is not over
    15.     Else
    16.         opt.BackColor = vbYellow '<- Color when the mouse is over
    17.     End If
    18. End Sub
    Add the above code to your Form and then for each of your Option Buttons you'll need to use the following code:
    VB Code:
    1. Private Sub opt1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     Call SetColor(opt1, X, Y)
    3. End Sub
    4.  
    5. Private Sub opt1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6.     Call SetCapture(opt1.hwnd)
    7. End Sub
    EDIT: Gee, am I a slow typer today.

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    Actually now that I think about it, it may be just as conveniant to chnage the colour when one of the option boxes is selected Before the user clicks on the button to submit there choice.

    If you could help me with that? Because it would probably make more sense.

    Also I forgot to mention that i'm using VBA if that makes a difference to this.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How do I make colour changes on mouse movement?

    Quote Originally Posted by Shmokin
    Also I forgot to mention that i'm using VBA if that makes a difference to this.
    Yes, that makes a HUGE difference.

    Moved to Office Development

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    Quote Originally Posted by Hack
    Yes, that makes a HUGE difference.

    Moved to Office Development
    Sorry hope you can still help with what I want.

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    Any Ideas? Come on people.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How do I make colour changes on mouse movement?

    Well did you try out the two code examples me and Hack gave you?

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    yeh I did but It wasn't very succesful

    I put the first bit of code you gave me in the form load event and it comes up saying for the first line "only comments may appear after ned sub, end funtion or end property"

    This bit,

    VB Code:
    1. Private Declare Function SetCapture Lib "user32.dll" ( _
    2.     ByVal hwnd As Long _
    3. ) As Long

  10. #10
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How do I make colour changes on mouse movement?

    All that code should be in the General Declaration section, above all other subs and functions.

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Question Re: How do I make colour changes on mouse movement?

    I put part of it into general declarations but this part wont go in so i left it in form load

    VB Code:
    1. Public Sub SetColor(opt As OptionButton, X As Single, Y As Single)
    2.     If GetCapture <> opt.hwnd Then
    3.         Call SetCapture(opt1.hwnd)
    4.     End If
    5.     If (X < 0 Or X > opt.Width) Or (Y < 0 Or Y > opt1.Height) Then
    6.         ReleaseCapture
    7.         opt.BackColor = vbGreen '<- Color you want when the mouse is not over
    8.     Else
    9.         opt.BackColor = vbYellow '<- Color when the mouse is over
    10.     End If

    Should it stay there or go into my module maybe?

    However since i put that bit of code in my declarations this error has occured on the option box code,

    VBCODE]Private Sub opt1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)[[/Highlight]

    it says "Procedure declaration does not match prodecure of event or procedure matching the same name"

    I was also thinking that If i change it so that it's not the mouse movement but just when the option box is selected will it be easier to do?

  12. #12
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: How do I make colour changes on mouse movement?

    If you read the first line of that code you'll notice that it says Public Sub... You can't put a Sub in another Sub which you do if you paste it into Form_Load.

    When it comes to MouseMove it has to do with that you origionally posted this question in the Classic VB Forum and I assumed that you where using VB6 and not VBA. You'll have to paste the code that exist in that sub into the correct MouseMove event of your option button.

  13. #13
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How do I make colour changes on mouse movement?

    Quote Originally Posted by Shmokin
    I was also thinking that If i change it so that it's not the mouse movement but just when the option box is selected will it be easier to do?
    Yes, this would be much easier to do. Copy the following procedure into the code for your form.

    VB Code:
    1. Private Sub Highlight_Control(Optional MarkControl As Control)
    2. Dim cMyControl As Control
    3.  
    4.     For Each cMyControl In Me.Controls
    5.         If Left(cMyControl.Name, 3) = "Opt" Then
    6.             cMyControl.BackColor = &H8000000F
    7.         End If
    8.     Next cMyControl
    9.    
    10.     If Not MarkControl Is Nothing Then
    11.         MarkControl.BackColor = RGB(255, 0, 0)
    12.     End If
    13. End Sub

    Now in the click event for each of your optionbuttons you would use the following, changing "OptionButton1" to the name of your option button.

    VB Code:
    1. Private Sub OptionButton1_Click()
    2.     Highlight_Control OptionButton1
    3. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    Thanks that seems to work, only thing is, is that when I click on a different button then the button i clicked previously on doesn't go back to it's normal colour.

    Any ideas?

    Thanks in advance.

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How do I make colour changes on mouse movement?

    Change
    cMyControl.BackColor = &H8000000F
    to what ever is the correct backcolor for your form - I was assuming htat you were using the default gray.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    Quote Originally Posted by DKenny
    Change
    cMyControl.BackColor = &H8000000F
    to what ever is the correct backcolor for your form - I was assuming htat you were using the default gray.
    I am just using the default grey but the option boxes still dont change back.

    I dont think it's accepting the if statement for the colour

    VB Code:
    1. For Each cMyControl In Me.Controls
    2.         If Left(cMyControl.Name, 3) = "Opt" Then
    3.             cMyControl.BackColor = vbBlue
    4.         End If
    5.     Next cMyControl

    Is there anything I have to chnage here?

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How do I make colour changes on mouse movement?

    What are your optionbuttons called?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: How do I make colour changes on mouse movement?

    Quote Originally Posted by DKenny
    What are your optionbuttons called?
    opt1, opt2, opt3 and opt4

  19. #19
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How do I make colour changes on mouse movement?

    Change
    If Left(cMyControl.Name, 3) = "Opt"
    to
    If Left(cMyControl.Name, 3) = "opt"

    i.e. change uppercase 'O' to lowercase 'o'
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

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