多个checkedlistbox中的事件处理

时间:2014-12-25 08:21:06

标签: vb.net checkedlistbox

我是vb.net的新手,我想是否有办法处理tabcontrol中的所有checkedlistbox事件

这是我的一个checkedlistbox的代码,我想将它粘贴到49个checkedlistboxes的其余部分是低效的

  Private Sub cbA1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles cbA1.SelectedIndexChanged

    For i = 0 To cbA1.Items.Count - 1
        cbA1.SetItemCheckState(i, CheckState.Unchecked)
    Next
    cbA1.SetItemCheckState(cbA1.SelectedIndex, CheckState.Checked)
'DISABLE CHKBOX AFTER PICKING THE CHOICE
    cbA1.Enabled = False
End Sub

2 个答案:

答案 0 :(得分:0)

 Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
        For i = 0 To CheckedListBox1.Items.Count - 1
            If CheckedListBox1.SelectedIndex = i Then
                CheckedListBox1.Items.RemoveAt(i)
            End If
        Next
    End Sub

答案 1 :(得分:0)

这就是我通常只用1个事件处理许多对象的方法。

Private Sub cb_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbA1.SelectedIndexChanged, cbA2.SelectedIndexChanged, cbA3.SelectedIndexChanged 'and add more checkedlistbox here separated by comma
   For i = 0 To sender.Items.Count - 1
      sender.SetItemCheckState(i, CheckState.Unchecked)
   Next

   sender.SetItemCheckState(sender.SelectedIndex, CheckState.Checked)

   'DISABLE CHKBOX AFTER PICKING THE CHOICE
   sender.Enabled = False
End Sub

如何获取checkedlistbox中的选中值,可以找到here

此代码将获取索引

For Each indexChecked In CheckedListBox1.CheckedIndices
   MessageBox.Show("Index " + itemChecked.ToString() + " is checked.")
Next

此代码将获得值

For Each itemChecked In CheckedListBox1.CheckedItems
   MessageBox.Show(itemChecked.ToString() & " is checked")
Next

更改' CheckedListBox1'发件人'并将其放在Sub cb_SelectedIndexChanged