Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: GEORGEFRT en 17 Marzo 2015, 00:55 am



Título: Pasar datos obtenidos de un Listbox a un archivo .txt
Publicado por: GEORGEFRT en 17 Marzo 2015, 00:55 am
Buen día, de un código que tengo me gustaría guardar el listado de un listbox a un archivo .txt con un botón de guardar, solo me falta saber la orden para guardar sin que dañe la estructura.
---------------------------------------------------------
Un webBrowser
Un Listbox
Un TextBox
Un Button

----------------------------------------CODIGO
Option Explicit On 
Option Strict On 
 
Public Class Form1 
 
    Private Sub Button1_Click( _ 
        ByVal sender As System.Object, _ 
        ByVal e As System.EventArgs) Handles Button1.Click 
        ListBox1.Items.Clear() 
        WebBrowser1.Navigate(TextBox1.Text.Trim) 
 
    End Sub 
 
    Private Sub WebBrowser1_DocumentCompleted( _ 
        ByVal sender As System.Object, _ 
        ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _ 
        Handles WebBrowser1.DocumentCompleted 
 
        If e.Url.ToString = String.Empty Or e.Url.ToString = "http:///" Then 
            Exit Sub 
        End If 
        Try 
            Me.Cursor = Cursors.WaitCursor 
 
            Call links() 
 
            Me.Cursor = Cursors.Default 
 
        Catch ex As Exception 
            Debug.Print(ex.Message.ToString) 
        End Try 
 
    End Sub 
 
    'Extrae los links 
    ' '''''''''''''''''''''''''''''''' 
    Private Sub links() 
 
        ListBox1.Items.Clear() 
        For i As Integer = 0 To WebBrowser1.Document.Links.Count - 1 
            ListBox1.Items.Add(WebBrowser1.Document.Links.Item(i).GetAttribute("href")) 
        Next 
 
    End Sub 
 
    Private Sub Form1_Load( _ 
        ByVal sender As System.Object, _ 
        ByVal e As System.EventArgs) Handles MyBase.Load 
        Button1.Text = " Cargar página y obtener links " 
        WebBrowser1.Visible = True 
        TextBox1.Text = "www.google.com" 
 
    End Sub 
End Class 


Título: Re: Pasar datos obtenidos de un Listbox a un archivo .txt
Publicado por: okik en 17 Marzo 2015, 17:24 pm
Código
  1. Private Sub Command1_Click()
  2. Dim Listado As String
  3. 'Intruduce la lista en 'Listado'
  4. For x = 0 To List1.ListCount - 1
  5. Listado = Listado & List1.List(x) & vbCrLf
  6. Next x
  7.  
  8. 'Guarda el listado
  9. GuardarArchivoTexto Listado, App.Path & "\Listado.txt"
  10. End Sub
  11.  
  12. Private Sub Form_Load()
  13. List1.AddItem "1. ABC"
  14. List1.AddItem "2. DEF"
  15. List1.AddItem "3. GHI"
  16. End Sub
  17.  
  18. Public Function GuardarArchivoTexto(ByVal strTexto As String, ByVal FileName As String)
  19. Dim File As Integer, Cont As Integer, cDato As String
  20. On Error GoTo EvitarError
  21.    File = FreeFile
  22. Open FileName For Binary As #File
  23. For Cont = 1 To Len(strTexto)
  24.    Put #File, Cont, strTexto
  25.    Close #File
  26. Next
  27. EvitarError:
  28. Select Case Err.Number
  29.    Case 91
  30.    Resume Next
  31. End Select
  32. End Function

Para cargar el archivo en el listBox puedes usar el código que facilito en esta pregunta (Respuesta no. 7#)
http://foro.elhacker.net/programacion_visual_basic/ayuda_con_saltos_de_linea-t430913.0.html (http://foro.elhacker.net/programacion_visual_basic/ayuda_con_saltos_de_linea-t430913.0.html)



Título: Re: Pasar datos obtenidos de un Listbox a un archivo .txt
Publicado por: GEORGEFRT en 18 Marzo 2015, 17:51 pm
NO se mucho de programación, me podrías decir como debe de estar todo completo, trate de meterlo y me crea muchos errores. y no responden para guardar los archivos listbox


Título: Re: Pasar datos obtenidos de un Listbox a un archivo .txt
Publicado por: okik en 18 Marzo 2015, 19:08 pm
Perdón no me di cuenta que lo tienes en VB.Net. Naturalmente que te da error, porque el código que he puesto es de VB6

Aquí lo tienes en VB.Net
Código
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Public Class Form1
  5.  
  6.    Private Sub Button1_Click( _
  7.        ByVal sender As System.Object, _
  8.        ByVal e As System.EventArgs) Handles Button1.Click
  9.        ListBox1.Items.Clear()
  10.        WebBrowser1.Navigate(TextBox1.Text.Trim)
  11.  
  12.    End Sub
  13.  
  14.    Private Sub WebBrowser1_DocumentCompleted( _
  15.        ByVal sender As System.Object, _
  16.        ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
  17.        Handles WebBrowser1.DocumentCompleted
  18.  
  19.        If e.Url.ToString = String.Empty Or e.Url.ToString = "http:///" Then
  20.            Exit Sub
  21.        End If
  22.        Try
  23.            Me.Cursor = Cursors.WaitCursor
  24.  
  25.            Call links()
  26.  
  27.            Me.Cursor = Cursors.Default
  28.  
  29.        Catch ex As Exception
  30.            Debug.Print(ex.Message.ToString)
  31.        End Try
  32.  
  33.    End Sub
  34.  
  35.    'Extrae los links
  36.    ' ''''''''''''''''''''''''''''''''
  37.    Private Sub links()
  38.  
  39.        ListBox1.Items.Clear()
  40.        For i As Integer = 0 To WebBrowser1.Document.Links.Count - 1
  41.            ListBox1.Items.Add(WebBrowser1.Document.Links.Item(i).GetAttribute("href"))
  42.        Next
  43.  
  44.    End Sub
  45.  
  46.    Private Sub Form1_Load( _
  47.        ByVal sender As System.Object, _
  48.        ByVal e As System.EventArgs) Handles MyBase.Load
  49.        Button1.Text = "Cargar página y obtener links "
  50.        Button2.Text = "Guardar lista"
  51.        WebBrowser1.Visible = True
  52.        TextBox1.Text = "www.google.com"
  53.  
  54.    End Sub
  55.  
  56.  
  57.    Public Shared Function Guardar_Archivo_Texto(ByVal strTexto As String, ByVal FileName As String) As Long
  58.        Guardar_Archivo_Texto = 0
  59.        Dim sw As New System.IO.StreamWriter(FileName)
  60.        sw.WriteLine(strTexto)
  61.        sw.Close()
  62.        Return Guardar_Archivo_Texto
  63.    End Function
  64.  
  65.    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  66.        Dim strListado As String = ""
  67.        'Intruduce la lista en 'Listado'
  68.        For x = 0 To ListBox1.Items.Count - 1
  69.            strListado = strListado & ListBox1.Items(x).ToString & vbCrLf
  70.        Next x
  71.        'Guarda la lista
  72.        Guardar_Archivo_Texto(strListado, System.Windows.Forms.Application.StartupPath & "\Listado.txt")
  73.        '////Notas:
  74.        'Guardar_Archivo_Texto (Lista, directorio y archivo)
  75.        'System.Windows.Forms.Application.StartupPath <---Es el directorio donde se encuentra  esta apliación
  76.    End Sub
  77. End Class




Esto es lo que he añadido:


Código
  1.    Public Shared Function Guardar_Archivo_Texto(ByVal strTexto As String, ByVal FileName As String) As Long
  2.        Guardar_Archivo_Texto = 0
  3.        Dim sw As New System.IO.StreamWriter(FileName)
  4.        sw.WriteLine(strTexto)
  5.        sw.Close()
  6.        Return Guardar_Archivo_Texto
  7.    End Function
  8.  
  9.    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  10.        Dim strListado As String = ""
  11.        'Intruduce la lista en 'Listado'
  12.        For x = 0 To ListBox1.Items.Count - 1
  13.            strListado = strListado & ListBox1.Items(x).ToString & vbCrLf
  14.        Next x
  15.        'Guarda la lista
  16.        Guardar_Archivo_Texto(strListado, System.Windows.Forms.Application.StartupPath & "\Listado.txt")
  17.        '////Notas:
  18.        'Guardar_Archivo_Texto (Lista, directorio y archivo)
  19.        'System.Windows.Forms.Application.StartupPath <---Es el directorio donde se encuentra  esta apliación
  20.    End Sub