Consulting

Results 1 to 5 of 5

Thread: Click event for a group of option buttons in a Frame

  1. #1

    Click event for a group of option buttons in a Frame

    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

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sub Frame_Click()
    MsgBox "you clicked an option button"
    End Sub
    Run the Form and click an option button
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    Attached Files Attached Files
    Last edited by Paul_Hossler; 08-22-2016 at 02:10 PM. Reason: Added info about intra-frame controls
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    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
    Attached Files Attached Files

  5. #5
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    1
    Location
    Quote Originally Posted by mikerickson View Post
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •