Recorrer un CheckBoxList, guardar valores y restaura los valores

Recorre el contenido de un checkboxlist y envia a "micampo" el valor de la casilla seleccionada separado por un gúión "-".

For i = 0 To miCheckBoxList1.Items.Count - 1
If miCheckBoxList1.Items(i).Selected Then 'SI ESTA SELECCIONADO
micampo.text += miCheckBoxList1.Items(i).Text & "-" 'ENVIA EL VALOR AL CAMPO
End If
Next

Resultado: camisa-playera-pantalón-

En caso de editar el registro y restaurar el valor en el CheckBoxList:
Dim restaura_cadena = micampo.text
Dim longitud = restaura_cadena.ToString.Length
Dim x, xx, xxx As Integer
Dim arreglo As New ArrayList()
Dim valor_cadena As String
xx = 1
xxx = 0
Dim cuenta_prendas As Integer = 0
'separa los registros y lo envia a un arreglo
For x = 1 To longitud
xxx = xxx + 1
If Mid(restaura_cadena, x, 1) = "-" Then
If xx = 1 Then
valor_cadena = Mid(restaura_cadena, xx, xxx - 1)
Else
valor_cadena = Mid(restaura_cadena, xx, xxx - 2)
End If
arreglo.Add(valor_cadena)
cuenta_prendas = cuenta_prendas + 1
xx = x + 2
xxx = 0
End If
Next

'selecciona la casilla del checkboxlist
Dim i, x1 As Integer
For x1 = 0 To cuenta_prendas - 1
For i = 0 To Me.CheckBoxList1.Items.Count - 1
If arreglo(x1) = Me.CheckBoxList1.Items(i).Text Then
Me.CheckBoxList1.Items(i).Selected = True
End If
Next
Next

Publicar un comentario

0 Comentarios