Results 1 to 5 of 5

Thread: [RESOLVED] [02/03] Using the Controls Collection of a groupbox?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Resolved [RESOLVED] [02/03] Using the Controls Collection of a groupbox?

    Hi all, I am trying to uncheck the currently checked radiobutton in a groupbox with 6 radiobuttons. I have this:
    VB Code:
    1. Dim RB As RadioButton
    2.         For Each RB In GroupBox4.Controls
    3.             RB.Checked = False
    4.         Next
    I get "Specified cast is not valid" in line "For Each RB In GroupBox4.Controls". What am I doing wrong, the editor isn't showing any errors.
    VB 2005, Win Xp Pro sp2

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Using the Controls Collection of a groupbox?

    You can't cast every control as a RadioButton if not every control IS a RadioButton. You need to check the type and only if it is a RadioButton can you then cast it as such and set its Checked property:
    VB Code:
    1. For Each ctl As Control In GroupBox4.Controls
    2.             If TypeOf ctl Is RadioButton Then
    3.                 DirectCast(ctl, RadioButton).Checked = False
    4.             End If
    5.         Next ctl
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [02/03] Using the Controls Collection of a groupbox?

    Quote Originally Posted by jmcilhinney
    if not every control IS a RadioButton.
    haha John.. you must be coding too much.. you are starting to write your sentences in VB syntax

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Using the Controls Collection of a groupbox?

    Me.HasLife = False
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [02/03] Using the Controls Collection of a groupbox?

    Ahh, I thought I can filter the collection further by providing what controls I am interested in. Thanks again, jmcilhinney (You must spread some Reputation around before giving it to jmcilhinney again )
    VB 2005, Win Xp Pro sp2

Posting Permissions

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



Click Here to Expand Forum to Full Width