2017-04-13 51 views
0

我的表单上有7个RadioButtons,一个组中有5个,另一个组中有2个。当他们在GroupBoxes时,检查其中一个RadioButtons没有取消选中同一GroupBox中的其他人,出于某种原因 - 我认为AutoCheck的值设置为False,这是因为否则当表单加载时,其中一个RadioButtons将被默认选中,我不希望发生这种情况。取消选中For Each控制循环中的所有RadioButtons

因此,取消选中其他RadioButtons相同GroupBox中,我试图写一个被称为子程序,在RadioButton_Click事件的值设置为True后。

For Each c As Control In Me.Controls 
     If TypeOf c Is RadioButton Then 
      If c.Name <> "rbtnAllSuppliers" AndAlso c.Name <> "rbtnIndividual" Then 
       If c.Name <> rbtn.Name Then 
        For Each rbt As RadioButton In Me.Controls 
         If rbt.Name = c.Name Then 
          rbt.Checked = False 
         End If 
        Next 
       End If 
      End If 
     End If 
Next 

此代码更无效。对于表单上的每个控件,如果它是一个RadioButton这不叫rbtnAllSuppliersrbtnIndividual(在2个独立的RadioButtons,并且它不是被设置为TrueRadioButton,然后将其设置为False

问题是,则计数2 LabelsRadioButtons,或至少他们铸造成这样,所以试图设置时,它的错误了``经过value of the Label`。

我又试图类似的东西

For Each c As Control In Me.Controls 
     If c.GetType Is GetType(RadioButton) Then 
      If c.Name <> "rbtnAllSuppliers" AndAlso c.Name <> "rbtnIndividual" Then 
       If c.Name <> rbtn.Name Then 
        AddHandler DirectCast(c, RadioButton).Checked, AddressOf c. 
       End If 
      End If 
     End If 
    Next 

但我不确定我会为AddressOf代码放置什么?代表在这里会做什么?如果我把AddressOf currentSubroutine(),肯定会创建一个无限循环?

有没有办法使用我不知道的类似方法取消选中所有RadioButtons

要实现这一目标,最好的方法是什么?

+1

你尝试'Me.Controls.OfType(中单选)'以避免达到任何标签? –

+0

@AF完美!似乎到目前为止完全按照要求工作,谢谢。 – Harambe

回答

0

通过使用Me.Controls.OfType(Of RadioButton)你可以避免检查您的循环中的类型,并确保每一个控件是一个RadioButton