View Full Version : Solved: autoformatting option buttons
MAIjah
11-10-2006, 10:46 AM
Hi all:
A bit of a 'brute force' programmer here. I'm using a form with mulitple groups of exclusive option buttons. I want to automatically format the selected option buttons and update the formatting as the selections are changed. For example, I want to highlight by changing the forecolor of the selected optionbutton label and grey out the other labels. I know how to do this for individual buttons but I have a total of 42 buttons on the form. There must be an easier way to do this formatting. Any help would be appreciated.
Thanks, John
Bob Phillips
11-10-2006, 11:23 AM
Assuming that your groups of buttons are within frames, do this.
First create a class, call it 'clsUserFormEvents', and add this code
Option Explicit
Public WithEvents mButtonGroup As msforms.OptionButton
Private Sub mButtonGroup_Click()
Dim collItem
For Each collItem In UserForm1.mcolEvents
If collItem.mButtonGroup.Parent.Name = mButtonGroup.Parent.Name Then
collItem.mButtonGroup.BackColor = &H8000000F
End If
Next collItem
mButtonGroup.BackColor = &H80000003
End Sub
Then in your userform, add this code to initialise the class
Public mcolEvents As Collection
Private Sub UserForm_Initialize()
Dim cBtnEvents As clsUserFormEvents
Dim ctl As msforms.Control
Set mcolEvents = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
Set cBtnEvents = New clsUserFormEvents
Set cBtnEvents.mButtonGroup = ctl
mcolEvents.Add cBtnEvents
End If
Next
End Sub
MAIjah
11-14-2006, 10:12 AM
Thanks:
It works! I'm not sure how the selected button is highlighted but it is. I've implemented this in a more complicated form successfully.
In layman's terms what is a class module and how are they typically used.
Thanks for your help
Cheer-JAH
Bob Phillips
11-14-2006, 01:28 PM
A class is usually just an implementation of a custom object, so if you want to create say a person object in code, you can create a person class, with attributes (properties) such as name, age, sex, and actions (methods) such as eat, drink, sleep, etc. That is a slightly fatuous example, but the principle is right. In general terms, a class is a custom object.
In this example, that array of controls is our object/calss, and by making all the buttons part of the object, we can have an action that all buttons trigger.
In the main, classes are not absolutely necessary, they can be achieved in other ways, but this sort of event handling can only be done with a class.
MAIjah
11-14-2006, 02:05 PM
XID:
Once again thanks for your code and explanation. I also saw your replies and others for a similar question on a previous post. Thanks for your patience. Unfortunately my time requirements don't allow me to spend more time here searching for programming solutions so I truly appreciate rapid solutions such as this.
Cheers, John
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.