PDA

View Full Version : Solved: help with changing properties of objects on a form



circaa
05-30-2006, 11:33 AM
Hi

What i'm trying to do is : i have many option buttons on a form. When a button is clicked, i want it's background color to change. And if the user changes his mind and clicks another one, the color of the first one must change back.

I know i could put the code in the click event, but since i have so many of em, that's ridiculous, there's gotta be another way

thanks

Joey

mvidas
05-30-2006, 12:14 PM
Hi Joey,

Welcome to vbax!

You will have to write event code for each option box, theres no way around that in vba, however you can have a function that changes the back color based on the value:Private Sub OptionButton1_Change()
ChangeBackColor OptionButton1
End Sub
Private Sub OptionButton2_Change()
ChangeBackColor OptionButton2
End Sub
Private Sub OptionButton3_Change()
ChangeBackColor OptionButton3
End Sub
Private Sub OptionButton4_Change()
ChangeBackColor OptionButton4
End Sub
Private Sub OptionButton5_Change()
ChangeBackColor OptionButton5
End Sub
Function ChangeBackColor(ByRef TheObj As Object) As Boolean
TheObj.BackColor = IIf(TheObj.Value, 65535, -2147483633)
End FunctionThe backcolor will be yellow (65535) when selected, and standard grey when not selected. See sample sheet for an example.

Let us know if we can help at all!
Matt

Aaron Blood
05-30-2006, 12:40 PM
In my limited testing, this worked fine...



Private Sub HiLite()
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.OptionButton Then
If Ctrl Then
Ctrl.BackColor = &HFFFF& 'yellow
Else
Ctrl.BackColor = &H8000000F 'gray
End If
End If
Next Ctrl
End Sub

Private Sub OptionButton1_Click()
HiLite
End Sub
Private Sub OptionButton2_Click()
HiLite
End Sub
Private Sub OptionButton3_Click()
HiLite
End Sub
Private Sub OptionButton4_Click()
HiLite
End Sub
Private Sub OptionButton5_Click()
HiLite
End Sub
Private Sub OptionButton6_Click()
HiLite
End Sub

circaa
05-30-2006, 12:44 PM
Thanks a lot Matt ! It worked great, I didn't know i could use the "change" event since it is not on the list of events. Are there other events not listed like this one ??

Thanks again

Norie
05-30-2006, 01:01 PM
Joey

Will it always be a particular colour for the selected option button?

mvidas
05-31-2006, 05:22 AM
It worked great, I didn't know i could use the "change" event since it is not on the list of events. Are there other events not listed like this one ??Hmmm.. it should be on the list of events! What about in your VBE, is 'Change' listed in the right-hand drop down when an option button is on the left?
As you can see there are definately more ways to do it as well. While Aaron's is nice because you don't have to send his function the control as an argument like mine, Norie's class example is even nicer since you don't need separate event code for each option. In any case, glad to help, and let us know if you have any questions!
Matt

circaa
05-31-2006, 05:28 AM
Norie

Yes, all of my option buttons must change colour when selected. Just like you did it. You guys are good.

thanks

Aaron Blood
05-31-2006, 06:48 AM
Hmmm.. it should be on the list of events! What about in your VBE, is 'Change' listed in the right-hand drop down when an option button is on the left?
As you can see there are definately more ways to do it as well. While Aaron's is nice because you don't have to send his function the control as an argument like mine, Norie's class example is even nicer since you don't need separate event code for each option. In any case, glad to help, and let us know if you have any questions!
Matt

Yeah, I liked Norie's too...