如何在Vb.net中执行期间禁用列表视图中的复选框..

时间:2016-11-30 09:37:56

标签: vb.net

在我的应用程序中,我希望当用户选择一个复选框时,所有其他复选框应该对他禁用,当他取消选中该复选框时,它将从列表视图中删除,其他禁用应该能够为他... 删除部分已经做了如此友好的指导我第二部分

3 个答案:

答案 0 :(得分:1)

我会提高@David的回答:

您必须为您的复选框注册CheckedChanged事件,例如在Form的Load事件中:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     For Each checkbox In Controls.OfType(Of CheckBox)
         AddHandler checkbox.CheckedChanged, AddressOf Checkbox_CheckedChanged
     Next
End Sub

当用户手动点击复选框时处理事件的Checkbox_CheckedChanged

Private Sub Checkbox_CheckedChanged(sender As Object, e As EventArgs)
    'Checkbox the user manually clicked
    Dim changedCheckbox = DirectCast(sender, CheckBox)

    If changedCheckbox.Checked Then
        For Each otherCheckbox In Controls.OfType(Of CheckBox)
            'We don´t want to change the state of the checkbox the user clicked on
            If changedCheckbox IsNot otherCheckbox Then
                otherCheckbox.Checked = False
            End If
        Next
    Else
        'Your part of deleting in listview
    End If
End Sub

答案 1 :(得分:0)

Checkbox1_CheckedChange方法就是您所需要的。

If Checkbox1.Checked = True Then
   Checkbox2.Enabled = False
   Checkbox3.Enabled = False
Else
  Checkbox2.Enabled = True
  Checkbox3.Enabled = True
End If

答案 2 :(得分:0)

也许是一个更好的功能:

Public Function DisableCheckBox(ByVal OriginalCheckBox As CheckBox)
    Try
        Dim Parent As Control = OriginalCheckBox.Parent

        For Each Chk As CheckBox In Parent.Controls.OfType(Of CheckBox)()
            If Not Chk.Name = OriginalCheckBox.Name Then
                Chk.Enabled = Not Chk.Enabled
            End If
        Next

        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

然后你只需拨打:

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    DisableCheckBox(sender)
End Sub