2013-09-22 39 views
-1

我想清除这种形式的数据。 你能告诉我这段代码有什么问题吗?清除表单中的数据。 Visual Basic

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
    Dim ctrl As Control 
    For Each ctrl In Me.Controls 
     If TypeOf ctrl Is RadioButton Then 
      RadioButton2.Checked = False 
     End If 
    Next 
End Sub 

回答

0

试试这个:

If TypeOf ctrl Is RadioButton Then 
    ctrl.Checked = False 
End If 

在你现在的代码,你通过所有的控制循环,但只更新RadioButton2的Checked属性。

+0

不工作。这个看起来很正常的代码不起作用。 ElseIf TypeOf ctrl是RadioButton Then CType(ctrl,RadioButton).Checked = False End If – Hfirst

0

下面的代码为我工作:

For Each ctrl As Control In Me.Controls 
    If TypeOf ctrl Is RadioButton Then 
    'reset the checked property to ALL your radioButtons  
    DirectCast(ctrl, RadioButton).Checked = False 
     End If 
    Next 

你会循环在窗体的所有控件。