Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have a windows forms with textboxes and group boxes containing radio buttons in them and a datetimepicker. I am trying to reset textboxes to spaces and group boxes with the radio buttons not to have any radio buttons selected when i press the Reset button on the form, however it is not working for the radio buttons. Only textboxes and datetimepicker is working.
Please help. the code so far is as under.

Thanks,
Tushar

VB
Private Sub ClearControls(ByVal frm As Form)
        Dim ctrl As Control
        For Each ctrl In frm.Controls
            If ctrl.GetType Is GetType(TextBox) Then
                ctrl.Text = ""
            ElseIf ctrl.GetType Is GetType(RadioButton) Then
                Dim radbut As RadioButton = ctrl
                radbut.Checked = False
            ElseIf ctrl.GetType Is GetType(DateTimePicker) Then
                Dim datetimepicker1 As DateTimePicker = ctrl
                datetimepicker1.ResetText()
            End If
        Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ClearControls(Me)
End Sub
Posted
Updated 15-Jul-12 2:17am
v2

It's pretty simple: the radio buttons are within a GroupBox: which means they are within the GroupBox.Controls list, rather than being in the main form Controls list. Change your ClearControls routine to accept a ControlCollection instead of a form, and recursively call it with the Controls list for all items within it.
 
Share this answer
 
Try this

C#
Private Sub ClearControls(ByVal frm As Form)
    Dim ctrl As Control
    For Each ctrl In frm.Controls
        If ctrl.GetType Is GetType(TextBox) Then
            ctrl.Text = ""
        ElseIf ctrl.GetType Is GetType(RadioButton) Then
            Dim radbut As RadioButton = ctrl
            radbut.Checked = False
        ElseIf ctrl.GetType Is GetType(DateTimePicker) Then
            Dim datetimepicker1 As DateTimePicker = ctrl
            datetimepicker1.ResetText()

        ElseIf ctrl.GetType Is GetType(GroupBox) Then
            ClearGroupedControls(ctrl)

        End If
    Next
End Sub

Private Sub ClearGroupedControls(ByVal gctrl As GroupBox)
    For Each ctrl In gctrl.Controls
        If ctrl.GetType Is GetType(RadioButton) Then
            Dim radbut As RadioButton = ctrl
            radbut.Checked = False
        End If
    Next
End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ClearControls(Me)
End Sub
 
Share this answer
 
Comments
tusharmathankar 15-Jul-12 9:39am    
Hi There,

Just a quick question, will this work if all the controls including textboxes and radio buttons within a group and all on a single panel. I guess not.???
Thanks,
Tushar
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900