Results 1 to 5 of 5

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,884
    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

Posting Permissions

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