Como utilizar el CheckedListBox
Para agregar Items al CheckedListBox solo se necesita una línea y en este caso supondremos que tenemos un form con un campo de texto, con el propósito de agregar Items al control.
Para agregar lo que contiene el TextBox se debería hacer lo siguiente:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
''En el evento del boton agregamos el código para agregar items al CheckedListBox Private Sub button1_Click(sender As Object, _ e As System.EventArgs) Handles button1.Click If textBox1.Text <> "" Then ''Se verifica si ya existe el texto para no repetir If checkedListBox1.CheckedItems.Contains(textBox1.Text) = False Then checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked) End If textBox1.Text = "" End If End Sub ''button1_Click |
La propiedad CheckState.Checked es la que indica si queremos que el item agregado aparezca chequeado o no. CheckState.Checked ó CheckState.Unchecked, también se pueden utilizar True o False.
Si queremos limpiar los Items chequeados solo debemos recorrer el control e ir quitando esta propiedad a cada uno de los campos.
|
1 2 3 4 5 |
Dim n As Integer For n = 0 To checkedListBox1.Items.Count - 1 checkedListBox1.SetItemChecked(n, False) Next |
Si queremos recorrer el CheckedListBox para salvar o validar los datos
|
1 2 3 4 5 6 7 |
Dim i As Integer Dim Valor as String For i = 0 To Me.chklCategorias.CheckedItems.Count - 1 Valor = chklCategorias.CheckedItems(i) Salvar(Valor) ''Valor contiene el contenido del item chequeado Next |
Si queremos por ejempo pasar los datos del CheckedListBox a un ListBox
|
1 2 3 4 5 6 7 8 9 10 |
'' Mueve los items chequeados desde el CheckedListBox a el listBox. Private Sub button2_Click(sender As Object, _ e As System.EventArgs) Handles button2.Click listBox1.Items.Clear() Dim i As Integer For i = 0 To checkedListBox1.CheckedItems.Count - 1 listBox1.Items.Add(checkedListBox1.CheckedItems(i)) Next i End Sub ''button2_Click |
Si queremos validar el evento de cuando un item es chequeado o deschequeado
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
''Valida si el control todavía tiene items chequeados Private Sub checkedListBox1_ItemCheck(sender As Object, _ e As ItemCheckEventArgs) Handles checkedListBox1.ItemCheck If e.NewValue = CheckState.Unchecked Then If checkedListBox1.CheckedItems.Count = 1 Then MsgBox("Todavía existen items activos") Else MsgBox("No existen items activos") End If Else MsgBox("Todavía existen items activos") End If End Sub ''checkedListBox1_ItemCheck |

ULTIMOS COMENTARIOS