PDA

View Full Version : Click event for a group of option buttons in a Frame



krishnak
08-22-2016, 09:19 AM
Hi All,

This is a general question not related to a specific procedure.
I created a Form in which I placed 6 option buttons within a Frame to select various ranges in the worksheet.
Does the Frame capture the event if any option button within that frame is clicked - say to select a specific option button?
We can write the code for each option button click event but does it have any relation to the Frame?
Of course, by placing groups of option buttons in different frames, we have the flexibility of selecting one option button in each frame.
Regards

SamT
08-22-2016, 09:55 AM
Sub Frame_Click()
MsgBox "you clicked an option button"
End Sub

Run the Form and click an option button

Paul_Hossler
08-22-2016, 11:53 AM
I think the Frame event will only fire if the non-OB part of the frame is clicked

The usual way is to define a clsOptionButton class



Option Explicit
Public WithEvents ctlOptionButton As MSForms.OptionButton

Private Sub ctlOptionButton_Change()
If ctlOptionButton.Value Then
MsgBox ctlOptionButton.Name & " is selected"
End If
End Sub




and hook the OB controls to the class, in an array or collection




Option Explicit

Dim aOptionButtons() As clsOptionButton

Private Sub CommandButton1_Click()
Me.Hide
Unload Me
End Sub

Private Sub Frame1_Click()
Dim I as long

MsgBox Me.Frame1.Caption

For i = 0 To Me.Frame1.Controls.Count - 1
MsgBox Me.Frame1.Controls(i).Name
Next I


End Sub

Private Sub UserForm_Initialize()
Dim oControl As Object, iCounter As Long

Me.OptionButton1.Value = True

ReDim aOptionButtons(1 To Me.Controls.Count)
For Each oControl In Me.Controls
If TypeName(oControl) = "OptionButton" Then
iCounter = iCounter + 1
Set aOptionButtons(iCounter) = New clsOptionButton
Set aOptionButtons(iCounter).ctlOptionButton = oControl
End If
Next
ReDim Preserve aOptionButtons(1 To iCounter)
End Sub




OB's are little tricky, since there are actually 2 change events that fire, the one that was True --> False and the one that was False --> True

Also, the controls in a Frame are accessable using the Frame.Controls, so if there is more than one Frame, you'll have to do some additional work

Sample WB

mikerickson
08-22-2016, 04:23 PM
A variation on Pauls class module would be for the OptionButton_Click event in the Class module to call a particular routine in the userfoms code module.

Create a class module, change its name to clsFrameOptionButton and put this code in its code module


' in clsFormOptionButton code module

Public WithEvents OptionButton As MSForms.OptionButton
Dim myColl As Collection

Private Sub OptionButton_Click()
On Error Resume Next
CallByName UFParent, OptionButton.Parent.Name & "OptionButton_Click", VbMethod
On Error GoTo 0
End Sub

Private Function UFParent() As Object
Set UFParent = OptionButton
On Error Resume Next
Do
Set UFParent = UFParent.Parent
Loop Until Err
On Error GoTo 0
End Function

Private Sub Class_Initialize()
Set myColl = New Collection
myColl.Add Item:=Me
End Sub


Then in the user form, in the Initialize event, create an instance of clsFormOptionButton for each OptionButton in the user form. Note that you don't have to store them anywhere, they are self-perpetuating.

If you want a particular Frame's option buttons to run a routine all you have to do is write a Public Sub, changing the name of the frame to match.

Public Sub Frame1OptionButton_Click()
' code
End Sub
Note that this must be declared a Public sub. Also note that the frame's ActiveControl will be the option button that was clicked. (this maybe different than the Userform's .ActiveControl)
In the attached workbook and user form you will see that the option buttons don't have to be in a form, they can be directly in that user form or they could be in the Page of a MultiPage. It all depends on the existence (or not) of the Public Sub (container's name)OptionButton_Click() code.

In the attached, option buttons in Frame1 or Frame2 or directly in the Userform each have a different event. However the option buttons in Frame3 aren't playing.

' in userform code module

Private Sub UserForm_Initialize()
Dim newFOB As clsFormOptionButton
Dim oneControl As MSForms.Control

For Each oneControl In Me.Controls
If TypeName(oneControl) = "OptionButton" Then
Set newFOB = New clsFormOptionButton
Set newFOB.OptionButton = oneControl
End If
Next oneControl

Set newFOB = Nothing
End Sub

Public Sub Frame1OptionButton_Click()
MsgBox Frame1.ActiveControl.Name & " clicked"
End Sub

Public Sub Frame2OptionButton_Click()
MsgBox Frame2.ActiveControl.Name & " in Frame2 was clicked"
End Sub

Public Sub Userform1OptionButton_Click()
MsgBox "You clicked a userform's direct child option button"
End Sub

Private Sub CommandButton1_Click()
Unload Me
End Sub

Irma
09-20-2017, 02:50 AM
A variation on Pauls class module would be for the OptionButton_Click event in the Class module to call a particular routine in the userfoms code module.

Create a class module, change its name to clsFrameOptionButton and put this code in its code module...[/CODE]

Man you are the best. I am trying to hide certain parts on the control if you click outside of it, while trying to avoid using WinAPI functions. This way I can easily hook each control on the form to the function which makes it invisible on click. Much <3!