Results 1 to 3 of 3

Thread: Clearing all radio buttons on a form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92

    Clearing all radio buttons on a form

    Hopefully this is something quick.

    I have a form with 4 group boxes. 2 of them have text boxes. I found how to clear them quick enough in past postings to this forum.

    The basic idea is that you have to loop through the controls to find the group box then loop to find the text boxes, like this:

    VB Code:
    1. For Each c1 As Control In Me.Controls
    2.             If TypeOf c1 Is GroupBox Then
    3.                 For Each c2 As Control In c1.Controls
    4.                     If TypeOf c2 Is TextBox Then
    5.                         c2.Text = Nothing
    6.                     End If
    7.                 Next
    8.             End If
    9.         Next

    So I tried to add another IF statement to uncheck the radio boxes. Something like:

    VB Code:
    1. If TypeOf c2 is RadioButton Then
    2.     c2.Checked = False
    3. End If

    but that doesn't work. Checked is not a parameter that you can change for a control.

    I only had 8 radio buttons, so I just did :

    VB Code:
    1. RadioButton1.Checked = False
    2.         RadioButton2.Checked = False
    3.         RadioButton3.Checked = False
    4.         RadioButton4.Checked = False
    5.         RadioButton5.Checked = False
    6.         RadioButton6.Checked = False
    7.         RadioButton7.Checked = False
    8.         RadioButton8.Checked = False

    But is there a better way?

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Your code would work if you just direcast it to radibutton . like this
    VB Code:
    1. For Each c1 As Control In Me.Controls
    2.             If TypeOf c1 Is GroupBox Then
    3.                 For Each c2 As Control In c1.Controls
    4.                     If TypeOf c2 Is RadioButton Then
    5.                         DirectCast(c2, RadioButton).Checked = False
    6.                     End If
    7.                 Next
    8.             End If
    9.         Next

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    Nice! I knew it would be easy. Thanks a lot.

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