Sigo con mis eternos rollos de los listbox :silbar: ,ya resuelto el problema de los iguales ahora tengo otra incógnita
si tengo un listbox con estos registros
1 2 3 *********correlativos
1 4 5
1 3 4
1 3 5
2 3 4*********correlativo
4 5 6 ********correlativo
6 7 8 ********correlativo
Y quiero eliminar los números correlativos como lo puedo hacer ya he visto 100 ejemplos de linq jejje pero solo son para bases de datos
luis
ListBox1.Items.Add("1 2 3")
ListBox1.Items.Add("1 4 5")
ListBox1.Items.Add("1 3 4")
ListBox1.Items.Add("1 3 5")
ListBox1.Items.Add("2 3 4")
ListBox1.Items.Add("4 5 6")
ListBox1.Items.Add("6 7 8")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)
ClearList.AddRange(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CDbl(Digito.Substring(2, 1)) = CDbl(Digito.Substring(0, 1)) + 1 And
CDbl(Digito.Substring(4, 1)) = CDbl(Digito.Substring(2, 1)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
Devuelve:
1 4 5
1 3 4
1 3 5
Simplemente hace una comparación de dígitos línea por línea:
Linea --> a b c
si b= a+1 y c = b+1 entonces borra la línea
A la hora de usar Substring, hay que tener en cuenta el lugar de los dígitos:
0]A[1]espacio[2]B[3]espacio[4]C
A está en la posición 0 --> (0, 1)
B está en la posición 2 --> (2, 1)
C está en la posición 4 --> (4, 1)
Gracias Lekim " PERO " :silbar: :silbar:
Probandolo me da error " La conversión de la cadena "," en el tipo 'Double' no es válida. "
If CDbl(Digito.Substring(2, 1)) = CDbl(Digito.Substring(0, 1)) + 1 And
CDbl(Digito.Substring(4, 1)) = CDbl(Digito.Substring(2, 1)) + 1 Then
"
Luis
Amos a ver ::)
Esa es la razón de que me haya molestado en explicar esto:
Simplemente hace una comparación de dígitos línea por línea:
Linea --> a b c
si b= a+1 y c = b+1 entonces borra la línea
A la hora de usar Substring, hay que tener en cuenta el lugar de los dígitos:
0]A[1]espacio[2]B[3]espacio[4]C
A está en la posición 0 --> (0, 1)
B está en la posición 2 --> (2, 1)
C está en la posición 4 --> (4, 1)
Si tu lo tienes puesto en el ListBox así ("1 ,2 ,3")
La posición de los dígitos cambia. Como yo no soy adivino y no se como lo tienes pues te he puesto el ejemplo en base a como lo has mostrado tu;
1 2 3 *********correlativos
1 4 5
1 3 4
1 3 5
2 3 4*********correlativo
4 5 6 ********correlativo
6 7 8 ********correlativo
¿Verdad que no lo has puesto así? 1 ,4 ,5. Pues eso.
Si copias mi código tal cual en un nuevo form y con un único listbox y lo pones en el evento load te funciona. Lo que pasa es que como he dicho tu lo dentrás así con las comas.
1 ,2 ,3
A ver así ::)
ListBox1.Items.Add("1, 2, 3")
ListBox1.Items.Add("1, 4, 5")
ListBox1.Items.Add("1, 3, 4")
ListBox1.Items.Add("1, 3, 5")
ListBox1.Items.Add("2, 3, 4")
ListBox1.Items.Add("4, 5, 6")
ListBox1.Items.Add("6, 7, 8")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)
ClearList.AddRange(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CDbl(Digito.Substring(3, 1)) = CDbl(Digito.Substring(0, 1)) + 1 And
CDbl(Digito.Substring(6, 1)) = CDbl(Digito.Substring(3, 1)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
0]A[1]espacio[2],[3]B[4]espacio[5],[6]C
A está en la posición 0 --> (0, 1)
B está en la posición 3 --> (3, 1)
C está en la posición 6 --> (6, 1)
Lo explicaré mejor:
Donde Dígito es una cadena tal como A ,B ,C
Digito.Substring(6, 1) devuelve UN carácter que se encuentra en la posición SEIS empezando desde la izquierda. O sea que si mueves el cursor 6 veces se pondrá justo delante de C en este caso [A ,B ,C] y devuelve justo ese carácter.
S2s
A ver que me he hecho un lío con las comas XD, pero funcionaba igual jeje
ListBox1.Items.Add("1 ,2 ,3")
ListBox1.Items.Add("1 ,4 ,5")
ListBox1.Items.Add("1 ,3 ,4")
ListBox1.Items.Add("1 ,3 ,5")
ListBox1.Items.Add("2 ,3 ,4")
ListBox1.Items.Add("4 ,5 ,6")
ListBox1.Items.Add("6 ,7 ,8")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)
ClearList.AddRange(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CDbl(Digito.Substring(3, 1)) = CDbl(Digito.Substring(0, 1)) + 1 And
CDbl(Digito.Substring(6, 1)) = CDbl(Digito.Substring(3, 1)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
También valdría CInt, creo que mejor incluso ya que son números pequeños:
ListBox1.Items.Add("1 ,2 ,3")
ListBox1.Items.Add("1 ,4 ,5")
ListBox1.Items.Add("1 ,3 ,4")
ListBox1.Items.Add("1 ,3 ,5")
ListBox1.Items.Add("2 ,3 ,4")
ListBox1.Items.Add("4 ,5 ,6")
ListBox1.Items.Add("6 ,7 ,8")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)
ClearList.AddRange(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CInt(Digito.Substring(3, 1)) = CInt(Digito.Substring(0, 1)) + 1 And
CInt(Digito.Substring(6, 1)) = CInt(Digito.Substring(3, 1)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
Cita de: Lekim en 29 Marzo 2016, 03:31 AM
También valdría CInt, creo que mejor incluso ya que son números pequeños:
ListBox1.Items.Add("1 ,2 ,3")
ListBox1.Items.Add("1 ,4 ,5")
ListBox1.Items.Add("1 ,3 ,4")
ListBox1.Items.Add("1 ,3 ,5")
ListBox1.Items.Add("2 ,3 ,4")
ListBox1.Items.Add("4 ,5 ,6")
ListBox1.Items.Add("6 ,7 ,8")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)
ClearList.AddRange(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CInt(Digito.Substring(3, 1)) = CInt(Digito.Substring(0, 1)) + 1 And
CInt(Digito.Substring(6, 1)) = CInt(Digito.Substring(3, 1)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
Vale ya son las tres y media jejje + algo de vino no veo ni la pantalla mañana lo revizo jejej a dormir
AAA y gracias
Luis
Sería mejor que lo pusieras así 01, 25, 06, para no tener que hacer la distinción de se es un dígito o dos, porque si no te va a dar error cuando hayan dos dígitos ya que cambia las posiciones.
ListBox1.Items.Add("01 ,02 ,03")
ListBox1.Items.Add("03 ,04 ,05")
ListBox1.Items.Add("15 ,35 ,45")
ListBox1.Items.Add("15 ,35 ,55")
ListBox1.Items.Add("25 ,35 ,45")
ListBox1.Items.Add("45 ,55 ,65")
ListBox1.Items.Add("57 ,58 ,59")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CInt(Digito.Substring(4, 2)) = CInt(Digito.Substring(0, 2)) + 1 And
CInt(Digito.Substring(8, 2)) = CInt(Digito.Substring(4, 2)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
Para ser más precisos se podría usar Regex, pero si fijo vas a usar dos dígitos, y pones los números del uno al 10 así, 01, 02, 03 .. no hace falta.
Otra forma sería como he comentado con Regex. En este caso no importa si el número tiene uno, dos, tres, etc, dígitos. Pero es importante establecer qué carácter o cadena habrá entre ellos en Pattern.
ListBox1.Items.Add("1 ,2 ,3") '<-Se borrará
ListBox1.Items.Add("3 ,4 ,5") '<-Se borrará
ListBox1.Items.Add("3 ,24 ,18")
ListBox1.Items.Add("15 ,35 ,45")
ListBox1.Items.Add("16 ,17 ,18") '<-Se borrará
ListBox1.Items.Add("45 ,55 ,65")
ListBox1.Items.Add("57 ,58 ,59") '<-Se borrará
ListBox1.Items.Add("346 ,348 ,350")
ListBox1.Items.Add("350 ,351 ,352") '<-Se borrará
Dim Pattern As String = " ,"
Dim Digito() As String
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)(ListBox1.Items.OfType(Of String))
For Each Item As String In ClearList
Digito = System.Text.RegularExpressions.Regex.Split(Item, Pattern)
If CInt(Digito(1)) = CInt(Digito(0)) + 1 And
CInt(Digito(2)) = CInt(Digito(1)) + 1 Then
ListBox1.Items.Remove(Item)
End If
Next
'//Lo que hace Regex.Split es devolver los valores que están entre espacio y coma --> " ,"
'//Si hay tres números las posiciones serían:
'//0[ ,]1[ ,]2
'//Digito(0) devuelve el primer número
'//Digito(1) devuelve el segundo número
'//Digito(2) devuelve el tercer número
Devuelve:
3 ,24 ,18
15 ,35 ,45
45 ,55 ,65
346 ,348 ,350
perfecto con este ;-) ;-) ;-)
ListBox1.Items.Add("01 ,02 ,03")
ListBox1.Items.Add("03 ,04 ,05")
ListBox1.Items.Add("15 ,35 ,45")
ListBox1.Items.Add("15 ,35 ,55")
ListBox1.Items.Add("25 ,35 ,45")
ListBox1.Items.Add("45 ,55 ,65")
ListBox1.Items.Add("57, 58 ,59")
'//Elimina combinaciones correlativas
Dim ClearList As New List(Of String)(ListBox1.Items.OfType(Of String))
For Each Digito As String In ClearList
If CInt(Digito.Substring(4, 2)) = CInt(Digito.Substring(0, 2)) + 1 And
CInt(Digito.Substring(8, 2)) = CInt(Digito.Substring(4, 2)) + 1 Then
ListBox1.Items.Remove(Digito)
End If
Next
Luis
Pero...
Como te he comentado también puedes usar REGEX, fíjate que el código parece el mismo pero no es igual. El uno usa Substring y el otro Regex.
Con regex da igual si lo pones así: 1 2 3 o así 1 ,2 ,3 o así 01, 02, 03 sólo tienes que establecer qué carácter o cadena hay entre los números en Pattern.
REGEX
ListBox1.Items.Add("01 ,02 ,03")
ListBox1.Items.Add("3 ,4 ,5")
ListBox1.Items.Add("15 ,35 ,45")
ListBox1.Items.Add("15 ,35 ,55")
ListBox1.Items.Add("25 ,35 ,45")
ListBox1.Items.Add("45 ,55 ,65")
ListBox1.Items.Add("57 ,58 ,59")
Dim Pattern As String = " ,"
Dim Digito() As String
Dim ClearList As New List(Of String)(ListBox1.Items.OfType(Of String))
For Each Item As String In ClearList
Digito = System.Text.RegularExpressions.Regex.Split(Item, Pattern)
If CInt(Digito(1)) = CInt(Digito(0)) + 1 And
CInt(Digito(2)) = CInt(Digito(1)) + 1 Then
ListBox1.Items.Remove(Item)
End If
Next
Fíjate que hay una línea en la que la he puesto sin el cero delante, y no da error:
ListBox1.Items.Add("3 ,4 ,5")
OTRAS CONDICIONES
Puedes cambiar las condiciones fácilmente.
Este código borra las combinaciones cuyo tercer número tiene dos valores mayor que el segundo.
Por ejemplo:
1 ,2 ,4
2, 3, 5
ListBox1.Items.Add("01 ,02 ,03")
ListBox1.Items.Add("3 ,4 ,5")
ListBox1.Items.Add("15 ,35 ,45")
ListBox1.Items.Add("15 ,35 ,55")
ListBox1.Items.Add("25 ,35 ,37")
ListBox1.Items.Add("45 ,55 ,57")
ListBox1.Items.Add("57 ,58 ,60")
Dim Pattern As String = " ,"
Dim Digito() As String
Dim ClearList As New List(Of String)(ListBox1.Items.OfType(Of String))
For Each Item As String In ClearList
Digito = System.Text.RegularExpressions.Regex.Split(Item, Pattern)
If CInt(Digito(2)) = CInt(Digito(1)) + 2 Then
ListBox1.Items.Remove(Item)
End If
Next
Este otro combina las condiciones anteriores, borra las líneas que tengan números correlativos y además las que tengan el tercer número dos veces mayor que el segundo.
ListBox1.Items.Add("01 ,02 ,03")
ListBox1.Items.Add("3 ,4 ,5")
ListBox1.Items.Add("15 ,35 ,45")
ListBox1.Items.Add("15 ,35 ,55")
ListBox1.Items.Add("25 ,35 ,37")
ListBox1.Items.Add("45 ,55 ,57")
ListBox1.Items.Add("57 ,58 ,60")
Dim Pattern As String = " ,"
Dim Digito() As String
Dim ClearList As New List(Of String)(ListBox1.Items.OfType(Of String))
For Each Item As String In ClearList
Digito = System.Text.RegularExpressions.Regex.Split(Item, Pattern)
If CInt(Digito(2)) = CInt(Digito(1)) + 2 Or
CInt(Digito(1)) = CInt(Digito(0)) + 1 And
CInt(Digito(2)) = CInt(Digito(1)) + 1 Then
ListBox1.Items.Remove(Item)
End If
Next
Espero te sirva.