Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim neworderlocation As String = "C:\Error\Error.log.txt"
  Dim datasave As IO.StreamWriter
  Dim sError As String
  datasave = IO.File.CreateText(neworderlocation)

  Dim i As Integer

  For i = 0 To lstErrors.SelectedItem.ToString


      sError = lstErrors.SelectedItem.ToString
      datasave.WriteLine(sError)


  Next i


  

  datasave.Close()
Posted
Comments
OriginalGriff 14-May-15 9:19am    
And?
Your problem is?
What does this do that you didn't expect, or not do that you did?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
Member 10250527 14-May-15 9:23am    
the problem is i cant write the data displayed on the listbox onto my textfile
ZurdoDev 14-May-15 9:33am    
Why can't you? It looks like you have code to do it.
OriginalGriff 14-May-15 9:34am    
OK...
Let's start again.
I can't see your screen, access your HDD, or read your mind - I only get what you write here to work with.

So I don't know what is in your listbox, I don't know what happens when you run that code, and I don't know what is in your text box afterwards (if anything). I don't even know when you are running that code, or if there is an exception!

So perhaps you might want to share some of that with me, because it would make helping you an awful lot easier... :laugh:

1 solution

Hi,

Try this:

VB
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ListBox1.Items.Add("Error1")
    ListBox1.Items.Add("Error2")
    ListBox1.Items.Add("Error3")
    ListBox1.Items.Add("Error4")
    ListBox1.Items.Add("Error5")

    'Open the Log File
    Dim FS As New FileStream("C:\ErrorLog.log", FileMode.OpenOrCreate, FileAccess.ReadWrite)

    'Assign to Stream Writer and Ensure Auto Flush is True
    Dim SW As New StreamWriter(FS)
    SW.AutoFlush = True

    'Loop Through Items
    For Each itm As String In ListBox1.Items
        'Write Item text to Stream
        SW.WriteLine(Now() & vbTab & itm)

    Next

    Try
        'Ensure All Closed
        SW.Dispose()
        FS.Close()
        FS.Dispose()
    Catch ex As Exception
        'Do Nothing
    End Try


End Sub

This produces this:
14/05/2015 14:30:07	Error1
14/05/2015 14:30:07	Error2
14/05/2015 14:30:07	Error3
14/05/2015 14:30:07	Error4
14/05/2015 14:30:07	Error5
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900