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
Private Sub Command1_Click()
Dim Listado As String
'Intruduce la lista en 'Listado'
For x = 0 To List1.ListCount - 1
Listado = Listado & List1.List(x) & vbCrLf
Next x
'Guarda el listado
GuardarArchivoTexto Listado, App.Path & "\Listado.txt"
End Sub
Private Sub Form_Load()
List1.AddItem "1. ABC"
List1.AddItem "2. DEF"
List1.AddItem "3. GHI"
End Sub
Public Function GuardarArchivoTexto(ByVal strTexto As String, ByVal FileName As String)
Dim File As Integer, Cont As Integer, cDato As String
On Error GoTo EvitarError
File = FreeFile
Open FileName For Binary As #File
For Cont = 1 To Len(strTexto)
Put #File, Cont, strTexto
Close #File
Next
EvitarError:
Select Case Err.Number
Case 91
Resume Next
End Select
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)
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
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
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 "
Button2.Text = "Guardar lista"
WebBrowser1.Visible = True
TextBox1.Text = "www.google.com"
End Sub
Public Shared Function Guardar_Archivo_Texto(ByVal strTexto As String, ByVal FileName As String) As Long
Guardar_Archivo_Texto = 0
Dim sw As New System.IO.StreamWriter(FileName)
sw.WriteLine(strTexto)
sw.Close()
Return Guardar_Archivo_Texto
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strListado As String = ""
'Intruduce la lista en 'Listado'
For x = 0 To ListBox1.Items.Count - 1
strListado = strListado & ListBox1.Items(x).ToString & vbCrLf
Next x
'Guarda la lista
Guardar_Archivo_Texto(strListado, System.Windows.Forms.Application.StartupPath & "\Listado.txt")
'////Notas:
'Guardar_Archivo_Texto (Lista, directorio y archivo)
'System.Windows.Forms.Application.StartupPath <---Es el directorio donde se encuentra esta apliación
End Sub
End Class
Esto es lo que he añadido:
Public Shared Function Guardar_Archivo_Texto(ByVal strTexto As String, ByVal FileName As String) As Long
Guardar_Archivo_Texto = 0
Dim sw As New System.IO.StreamWriter(FileName)
sw.WriteLine(strTexto)
sw.Close()
Return Guardar_Archivo_Texto
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strListado As String = ""
'Intruduce la lista en 'Listado'
For x = 0 To ListBox1.Items.Count - 1
strListado = strListado & ListBox1.Items(x).ToString & vbCrLf
Next x
'Guarda la lista
Guardar_Archivo_Texto(strListado, System.Windows.Forms.Application.StartupPath & "\Listado.txt")
'////Notas:
'Guardar_Archivo_Texto (Lista, directorio y archivo)
'System.Windows.Forms.Application.StartupPath <---Es el directorio donde se encuentra esta apliación
End Sub