|
-
Feb 15th, 2006, 09:03 AM
#1
Thread Starter
Member
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:
Private Sub opt1_MouseMove()
Let opt1.BackColor = vbYellow
Let opt2.BackColor = vbGreen
Let opt3.BackColor = vbGreen
Let opt4.BackColor = vbGreen
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.
-
Feb 15th, 2006, 09:09 AM
#2
Re: How do I make colour changes on mouse movement?
VB Code:
Option Explicit
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Sub Option1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X < 0) Or (Y < 0) Or (X > Option1.Width) Or (Y > Option1.Height) Then
ReleaseCapture
Option1.BackColor = &H8000000F
ElseIf GetCapture() <> Option1.hwnd Then
SetCapture Option1.hwnd
Option1.BackColor = vbGreen
End If
End Sub
-
Feb 15th, 2006, 09:19 AM
#3
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:
Private Declare Function SetCapture Lib "user32.dll" ( _
ByVal hwnd As Long _
) As Long
Private Declare Function ReleaseCapture Lib "user32.dll" () As Long
Private Declare Function GetCapture Lib "user32.dll" () As Long
Public Sub SetColor(opt As OptionButton, x As Single, y As Single)
If GetCapture <> opt.hwnd Then
Call SetCapture(opt.hwnd)
End If
If (x < 0 Or x > opt.Width) Or (y < 0 Or y > opt.Height) Then
ReleaseCapture
opt.BackColor = vbGreen '<- Color you want when the mouse is not over
Else
opt.BackColor = vbYellow '<- Color when the mouse is over
End If
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:
Private Sub opt1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call SetColor(opt1, X, Y)
End Sub
Private Sub opt1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call SetCapture(opt1.hwnd)
End Sub
EDIT: Gee, am I a slow typer today.
-
Feb 15th, 2006, 09:25 AM
#4
Thread Starter
Member
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.
-
Feb 15th, 2006, 09:28 AM
#5
Re: How do I make colour changes on mouse movement?
 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
-
Feb 15th, 2006, 09:31 AM
#6
Thread Starter
Member
Re: How do I make colour changes on mouse movement?
 Originally Posted by Hack
Yes, that makes a HUGE difference.
Moved to Office Development
Sorry hope you can still help with what I want.
-
Feb 15th, 2006, 02:32 PM
#7
Thread Starter
Member
Re: How do I make colour changes on mouse movement?
Any Ideas? Come on people.
-
Feb 15th, 2006, 03:10 PM
#8
Re: How do I make colour changes on mouse movement?
Well did you try out the two code examples me and Hack gave you?
-
Feb 15th, 2006, 03:24 PM
#9
Thread Starter
Member
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:
Private Declare Function SetCapture Lib "user32.dll" ( _
ByVal hwnd As Long _
) As Long
-
Feb 15th, 2006, 03:28 PM
#10
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.
-
Feb 15th, 2006, 03:43 PM
#11
Thread Starter
Member
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:
Public Sub SetColor(opt As OptionButton, X As Single, Y As Single)
If GetCapture <> opt.hwnd Then
Call SetCapture(opt1.hwnd)
End If
If (X < 0 Or X > opt.Width) Or (Y < 0 Or Y > opt1.Height) Then
ReleaseCapture
opt.BackColor = vbGreen '<- Color you want when the mouse is not over
Else
opt.BackColor = vbYellow '<- Color when the mouse is over
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?
-
Feb 15th, 2006, 03:50 PM
#12
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.
-
Feb 15th, 2006, 04:03 PM
#13
Re: How do I make colour changes on mouse movement?
 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:
Private Sub Highlight_Control(Optional MarkControl As Control)
Dim cMyControl As Control
For Each cMyControl In Me.Controls
If Left(cMyControl.Name, 3) = "Opt" Then
cMyControl.BackColor = &H8000000F
End If
Next cMyControl
If Not MarkControl Is Nothing Then
MarkControl.BackColor = RGB(255, 0, 0)
End If
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:
Private Sub OptionButton1_Click()
Highlight_Control OptionButton1
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Feb 15th, 2006, 04:10 PM
#14
Thread Starter
Member
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.
-
Feb 15th, 2006, 04:14 PM
#15
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 
-
Feb 15th, 2006, 04:21 PM
#16
Thread Starter
Member
Re: How do I make colour changes on mouse movement?
 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:
For Each cMyControl In Me.Controls
If Left(cMyControl.Name, 3) = "Opt" Then
cMyControl.BackColor = vbBlue
End If
Next cMyControl
Is there anything I have to chnage here?
-
Feb 15th, 2006, 04:22 PM
#17
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 
-
Feb 15th, 2006, 04:23 PM
#18
Thread Starter
Member
Re: How do I make colour changes on mouse movement?
 Originally Posted by DKenny
What are your optionbuttons called?
opt1, opt2, opt3 and opt4
-
Feb 15th, 2006, 04:25 PM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|