• Iterating through Options Group (VB6)

    Author
    Topic
    #408518

    I have a series of option buttons in a frame. I want to iterate through each option button, test it for a value or set a value. There doesn’t appear to be a controls collection associated with the frame. How is this done? For the entire form I woudl dim a control and do something like

    for each ctl in controls
    ‘do something
    next ctl

    How would I isolate it only to the controls in the frame?

    Viewing 1 reply thread
    Author
    Replies
    • #863125

      The Frame control does not have a Controls collection; you’d have to use the Container property of the individual controls on form. Example:

      Private Sub Command1_Click()
      Dim ctl As Control

      For Each ctl In Form1.Controls
      If ctl.Container Is Frame1 Then
      Debug.Print ctl.Name
      End If
      Next ctl

      Set ctl = Nothing

      End Sub

      Test results:

      Option3
      Option2
      Option1

      As noted in Object Browser:

      Property Container As object – Member of VB.OptionButton. Returns the container of an object.

      According to VB Help for Container property, the following controls can contain other controls:

      – Frame control
      – PictureBox control
      – SSTab control

      See the documentation for more details.

      HTH

      • #863489

        Mark,

        Thanks, that did help. What I’m finding is that at a certain point in the loop it raises an error that it doesn’t support that property or method. When I put error handling around it and do a resume next, I find it is considering the menu items as part of the frame, which is no where near the frame and, well I didn’t write this code originally, but there would be no reason to make it part of the frame.

        I can get around this because the developer at least had the good sense to prefix all the names with “opt” so I can test for that string. Just curious why it is considering the menu items as part of the frame’s container collection.

        Mike

      • #863490

        Mark,

        Thanks, that did help. What I’m finding is that at a certain point in the loop it raises an error that it doesn’t support that property or method. When I put error handling around it and do a resume next, I find it is considering the menu items as part of the frame, which is no where near the frame and, well I didn’t write this code originally, but there would be no reason to make it part of the frame.

        I can get around this because the developer at least had the good sense to prefix all the names with “opt” so I can test for that string. Just curious why it is considering the menu items as part of the frame’s container collection.

        Mike

    • #863126

      The Frame control does not have a Controls collection; you’d have to use the Container property of the individual controls on form. Example:

      Private Sub Command1_Click()
      Dim ctl As Control

      For Each ctl In Form1.Controls
      If ctl.Container Is Frame1 Then
      Debug.Print ctl.Name
      End If
      Next ctl

      Set ctl = Nothing

      End Sub

      Test results:

      Option3
      Option2
      Option1

      As noted in Object Browser:

      Property Container As object – Member of VB.OptionButton. Returns the container of an object.

      According to VB Help for Container property, the following controls can contain other controls:

      – Frame control
      – PictureBox control
      – SSTab control

      See the documentation for more details.

      HTH

    Viewing 1 reply thread
    Reply To: Iterating through Options Group (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: