Hola
tengo cuatro listbox
ListBox1
ListBox2
ListBox3
ListBox4
y tengo esta rutina que me grava pero en uno solo y quisera saber si se pueden grabar los cuatro pero de esta forma
ListBox1 ListBox2 ListBox3 ListBox4
74 12 25 88
55 99 33 90
99 88 44 66
codigo
Dim rutaFichero As String
Dim i As Integer
rutaFichero = Path.Combine(Application.StartupPath, "Prueba.txt")
Dim fichero As New IO.StreamWriter(rutaFichero)
For i = 0 To ListBox1.Items.Count - 1
fichero.WriteLine(ListBox1.Items(i))
Next
fichero.Close()
saludos
Luis
hace lo mismo que estas haciendo pero para los 4 y listo.
pero en vez de poner 4 veces el mismo codigo, create una funcion a la cual la puedas llamar pasandole como parametro el listbox a guardar.
Cita de: seba123neo en 4 Febrero 2015, 16:28 PM
hace lo mismo que estas haciendo pero para los 4 y listo.
pero en vez de poner 4 veces el mismo codigo, create una funcion a la cual la puedas llamar pasandole como parametro el listbox a guardar.
gracias pero segun entendi lo que me pones es grabar cada ves cada listbox y yo nesecito es gravarlos al mismo tiempo en el mismo txt en ese orden
gracias
Luis
Yo estoy aprendiendo recien, pero talves podrias hacerlo con String.Format
fichero.WriteLine(String.Format("{0} {1} {2} {3}", ListBox1.Items(i), ListBox2.Items(i), ListBox3.Items(i), ListBox4.Items(i)))
Cita de: OscarCadenas_91 en 4 Febrero 2015, 16:59 PM
Yo estoy aprendiendo recien, pero talves podrias hacerlo con String.Format
fichero.WriteLine(String.Format("{0} {1} {2} {3}", ListBox1.Items(i), ListBox2.Items(i), ListBox3.Items(i), ListBox4.Items(i)))
Perfecto gracias mil puntos para ti :)
codigo
Dim rutaFichero As String
Dim i As Integer
rutaFichero = Path.Combine(Application.StartupPath, "Prueba.txt")
Dim fichero As New IO.StreamWriter(rutaFichero)
For i = 0 To ListBox1.Items.Count - 1
fichero.WriteLine(String.Format("{0} {1} {2} {3}", ListBox1.Items(i), ListBox2.Items(i), ListBox3.Items(i), ListBox4.Items(i)))
' fichero.WriteLine(ListBox1.Items(i))
Next
fichero.Close()
Luis
Hola Luis
Simplemente podrías serializar los datos en un archivo binario si tu intención es cargarlo de nuevo al programa, es algo muy facil, pero creo que no es el caso lo que quieres hacer, así que esto requiere mucha más escritura.
He escrito este código hardcodeado, optimizado para un conjunto de listas con cantidad de items indefinida en cada lista, y con longitud de item indefinida.
Nota: No lo he testeado mucho, quizás pueda haber agún error en ciertas circunstancias.
Output:
ListBox1 ListBox2 ListBox3 ListBox4
******** ******** ******** ********
a1qwerty a2 a3 a4
qwertyqw
erty
-------- -------- -------- --------
b1 b2 b3 b4
-------- -------- -------- --------
c1 c3
-------- -------- -------- --------
d3
-------- -------- -------- --------
Source:
Imports System.IO
Imports System.Text
Public Class TestForm
Private Sub Test() Handles Button1.Click
Dim lbs As ListBox() = {ListBox1, ListBox2, ListBox3, ListBox4}
Dim spacing As Integer = 6 ' The horizontal spacing between columns.
Dim filaPath As String = Path.Combine(Application.StartupPath, "Test.txt")
Dim item1Parts As IEnumerable(Of String) = {}
Dim item2Parts As IEnumerable(Of String) = {}
Dim item3Parts As IEnumerable(Of String) = {}
Dim item4Parts As IEnumerable(Of String) = {}
Dim maxItemCount As Integer
Dim maxItemPartCount As Integer
Dim sb As New StringBuilder
' Get the max item count in listboxes.
maxItemCount = (From lb As ListBox In lbs
Order By lb.Items.Count Descending
Select lb.Items.Count).First
' Write column names.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
lbs(0).Name, lbs(1).Name,
lbs(2).Name, lbs(3).Name))
' Write column separator.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
New String("*"c, lbs(0).Name.Length),
New String("*"c, lbs(1).Name.Length),
New String("*"c, lbs(2).Name.Length),
New String("*"c, lbs(3).Name.Length)))
' Iterate listbox items.
For index As Integer = 0 To (maxItemCount - 1)
' If item at index exist...
If lbs(0).Items.Count > index Then
item1Parts = SplitByLength(lbs(0).Items(index).ToString, lbs(0).Name.Length)
End If
If lbs(1).Items.Count > index Then
item2Parts = SplitByLength(lbs(1).Items(index).ToString, lbs(1).Name.Length)
End If
If lbs(2).Items.Count > index Then
item3Parts = SplitByLength(lbs(2).Items(index).ToString, lbs(2).Name.Length)
End If
If lbs(3).Items.Count > index Then
item4Parts = SplitByLength(lbs(3).Items(index).ToString, lbs(3).Name.Length)
End If
If (item1Parts.Count > 1) OrElse
(item2Parts.Count > 1) OrElse
(item3Parts.Count > 1) OrElse
(item4Parts.Count > 1) Then
' Get the max item count in itemParts.
maxItemPartCount = (From col As IEnumerable(Of String)
In {item1Parts, item2Parts, item3Parts, item4Parts}
Order By col.Count Descending
Select col.Count).First
For x As Integer = 0 To (maxItemPartCount - 1)
' Write multiple items rows.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
If(item1Parts.Count <= x, String.Empty,
item1Parts(x) & New String(" "c, lbs(0).Name.Length - item1Parts(x).Length)),
If(item2Parts.Count <= x, String.Empty,
item2Parts(x) & New String(" "c, lbs(1).Name.Length - item2Parts(x).Length)),
If(item3Parts.Count <= x, String.Empty,
item3Parts(x) & New String(" "c, lbs(2).Name.Length - item3Parts(x).Length)),
If(item4Parts.Count <= x, String.Empty,
item4Parts(x) & New String(" "c, lbs(3).Name.Length - item4Parts(x).Length))))
Next
Else
' Write simgle items row.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
If(lbs(0).Items.Count <= index,
String.Empty & New String(" "c, lbs(0).Name.Length),
item1Parts.First & New String(" "c, lbs(0).Name.Length - item1Parts.First.Length)),
If(lbs(1).Items.Count <= index,
String.Empty & New String(" "c, lbs(1).Name.Length),
item2Parts.First & New String(" "c, lbs(1).Name.Length - item2Parts.First.Length)),
If(lbs(2).Items.Count <= index,
String.Empty & New String(" "c, lbs(2).Name.Length),
item3Parts.First & New String(" "c, lbs(2).Name.Length - item3Parts.First.Length)),
If(lbs(3).Items.Count <= index,
String.Empty & New String(" "c, lbs(3).Name.Length),
item4Parts.First & New String(" "c, lbs(3).Name.Length - item4Parts.First.Length))))
End If
' Write horizontal grid.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
New String("-"c, lbs(0).Name.Length),
New String("-"c, lbs(1).Name.Length),
New String("-"c, lbs(2).Name.Length),
New String("-"c, lbs(3).Name.Length)))
Next index
File.WriteAllText(filaPath, sb.ToString, Encoding.Default)
sb.Clear()
Process.Start(filaPath)
End Sub
' Split By Length
' By Elektro
'
''' <summary>
''' Splits a string by the specified character length.
''' </summary>
''' <param name="str">The string to split.</param>
''' <param name="length">The character length.</param>
''' <returns>IEnumerable(Of System.String).</returns>
Private Function SplitByLength(ByVal str As String,
ByVal length As Integer) As IEnumerable(Of String)
Dim stringParts As New List(Of String)
If str.Length > length Then
Do Until str.Length <= length
stringParts.Add(str.Substring(0, length))
str = str.Remove(0, length)
Loop
' Add the last missing part (if any).
If Not String.IsNullOrEmpty(str) Then
stringParts.Add(str)
End If
Else
stringParts.Add(str)
End If
Return stringParts
End Function
End Class
¿ tiene que quedar asi en ese formato si o si ?
porque si es grsabarlo y despues recuperarlo, podes usar simplemente un archivo .ini, es muy facil grabarlo y despues volver a recuperar los datos.
Cita de: seba123neo en 4 Febrero 2015, 19:13 PM
¿ tiene que quedar asi en ese formato si o si ?
porque si es grsabarlo y despues recuperarlo, podes usar simplemente un archivo .ini, es muy facil grabarlo y despues volver a recuperar los datos.
de gravarlo tiene que ser asi ya que debo imprimir el txt para otros menesteres pero con el codigo que me suministra nuestro amigo Elektro que por cierto es mas largo que mi propio programa jajajja me sobra :)
Gracias a todos
Luis
Cita de: Eleкtro en 4 Febrero 2015, 18:42 PM
Hola Luis
Simplemente podrías serializar los datos en un archivo binario si tu intención es cargarlo de nuevo al programa, es algo muy facil, pero creo que no es el caso lo que quieres hacer, así que esto requiere mucha más escritura.
He escrito este código hardcodeado, optimizado para un conjunto de listas con cantidad de items indefinida en cada lista, y con longitud de item indefinida.
Nota: No lo he testeado mucho, quizás pueda haber agún error en ciertas circunstancias.
Output:
ListBox1 ListBox2 ListBox3 ListBox4
******** ******** ******** ********
a1qwerty a2 a3 a4
qwertyqw
erty
-------- -------- -------- --------
b1 b2 b3 b4
-------- -------- -------- --------
c1 c3
-------- -------- -------- --------
d3
-------- -------- -------- --------
Source:
Imports System.IO
Imports System.Text
Public Class TestForm
Private Sub Test() Handles Button1.Click
Dim lbs As ListBox() = {ListBox1, ListBox2, ListBox3, ListBox4}
Dim spacing As Integer = 6 ' The horizontal spacing between columns.
Dim filaPath As String = Path.Combine(Application.StartupPath, "Test.txt")
Dim item1Parts As IEnumerable(Of String) = {}
Dim item2Parts As IEnumerable(Of String) = {}
Dim item3Parts As IEnumerable(Of String) = {}
Dim item4Parts As IEnumerable(Of String) = {}
Dim maxItemCount As Integer
Dim maxItemPartCount As Integer
Dim sb As New StringBuilder
' Get the max item count in listboxes.
maxItemCount = (From lb As ListBox In lbs
Order By lb.Items.Count Descending
Select lb.Items.Count).First
' Write column names.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
lbs(0).Name, lbs(1).Name,
lbs(2).Name, lbs(3).Name))
' Write column separator.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
New String("*"c, lbs(0).Name.Length),
New String("*"c, lbs(1).Name.Length),
New String("*"c, lbs(2).Name.Length),
New String("*"c, lbs(3).Name.Length)))
' Iterate listbox items.
For index As Integer = 0 To (maxItemCount - 1)
' If item at index exist...
If lbs(0).Items.Count > index Then
item1Parts = SplitByLength(lbs(0).Items(index).ToString, lbs(0).Name.Length)
End If
If lbs(1).Items.Count > index Then
item2Parts = SplitByLength(lbs(1).Items(index).ToString, lbs(1).Name.Length)
End If
If lbs(2).Items.Count > index Then
item3Parts = SplitByLength(lbs(2).Items(index).ToString, lbs(2).Name.Length)
End If
If lbs(3).Items.Count > index Then
item4Parts = SplitByLength(lbs(3).Items(index).ToString, lbs(3).Name.Length)
End If
If (item1Parts.Count > 1) OrElse
(item2Parts.Count > 1) OrElse
(item3Parts.Count > 1) OrElse
(item4Parts.Count > 1) Then
' Get the max item count in itemParts.
maxItemPartCount = (From col As IEnumerable(Of String)
In {item1Parts, item2Parts, item3Parts, item4Parts}
Order By col.Count Descending
Select col.Count).First
For x As Integer = 0 To (maxItemPartCount - 1)
' Write multiple items rows.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
If(item1Parts.Count <= x, String.Empty,
item1Parts(x) & New String(" "c, lbs(0).Name.Length - item1Parts(x).Length)),
If(item2Parts.Count <= x, String.Empty,
item2Parts(x) & New String(" "c, lbs(1).Name.Length - item2Parts(x).Length)),
If(item3Parts.Count <= x, String.Empty,
item3Parts(x) & New String(" "c, lbs(2).Name.Length - item3Parts(x).Length)),
If(item4Parts.Count <= x, String.Empty,
item4Parts(x) & New String(" "c, lbs(3).Name.Length - item4Parts(x).Length))))
Next
Else
' Write simgle items row.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
If(lbs(0).Items.Count <= index,
String.Empty & New String(" "c, lbs(0).Name.Length),
item1Parts.First & New String(" "c, lbs(0).Name.Length - item1Parts.First.Length)),
If(lbs(1).Items.Count <= index,
String.Empty & New String(" "c, lbs(1).Name.Length),
item2Parts.First & New String(" "c, lbs(1).Name.Length - item2Parts.First.Length)),
If(lbs(2).Items.Count <= index,
String.Empty & New String(" "c, lbs(2).Name.Length),
item3Parts.First & New String(" "c, lbs(2).Name.Length - item3Parts.First.Length)),
If(lbs(3).Items.Count <= index,
String.Empty & New String(" "c, lbs(3).Name.Length),
item4Parts.First & New String(" "c, lbs(3).Name.Length - item4Parts.First.Length))))
End If
' Write horizontal grid.
sb.AppendLine(String.Format("{1}{0}{2}{0}{3}{0}{4}",
New String(" "c, spacing),
New String("-"c, lbs(0).Name.Length),
New String("-"c, lbs(1).Name.Length),
New String("-"c, lbs(2).Name.Length),
New String("-"c, lbs(3).Name.Length)))
Next index
File.WriteAllText(filaPath, sb.ToString, Encoding.Default)
sb.Clear()
Process.Start(filaPath)
End Sub
' Split By Length
' By Elektro
'
''' <summary>
''' Splits a string by the specified character length.
''' </summary>
''' <param name="str">The string to split.</param>
''' <param name="length">The character length.</param>
''' <returns>IEnumerable(Of System.String).</returns>
Private Function SplitByLength(ByVal str As String,
ByVal length As Integer) As IEnumerable(Of String)
Dim stringParts As New List(Of String)
If str.Length > length Then
Do Until str.Length <= length
stringParts.Add(str.Substring(0, length))
str = str.Remove(0, length)
Loop
' Add the last missing part (if any).
If Not String.IsNullOrEmpty(str) Then
stringParts.Add(str)
End If
Else
stringParts.Add(str)
End If
Return stringParts
End Function
End Class
Haberme leido la mente no hubiera sido tan asombrosamente . Mil puntos para ti tambien elektro gracias menudo codigo :) Funciona de maravilla
Luis
Pero claro ya Elektro se estaria preguntando que esta ves fue facil ;)
pero no jejejje te tengo una pregunta ? se puede cambiar esto y poner otro nombre por ejemplo
ListBox1 ListBox2 ListBox3 ListBox4
******** ******** ******** ********
y poner
resultado 1 resultado2 resultado3 resultado4
******** ******** ******** ********
Saludos
Luis
Abajo donde esta comentado: Write column names
Ahi se coloca los nombres de los listbox, puedes cambiar los nombres de los listbox a resultado1, resultado2, etc
Solucionado jejej propiedades de Listbox / name y listo :)
a veces me auto soluciono las cosas
Luis