dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Write data from ListBox to File - exclude <CR><LF> when writing

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    16

    Resolved [RESOLVED] Write data from ListBox to File - exclude <CR><LF> when writing

    Hi, new problem:

    I'm writing data from a ListBox to a file.

    Code:
        Open "C:\LogFiles\" + TxtBxFileName.text For Append As #1
            For i = 0 To ListData.ListCount - 1
                ListData.Selected(i) = True
                Print #1, ListData.List(ListData.ListIndex)
            Next
        Close #1
    It seems like this function appends a <CR> and <LF> (carriage return+line feed) to the end of each line it writes from the ListBox. I do not want this. I want to write the data from the ListBox exactly as it is, line by line without inserting any <cr> nor <lf>.

    Any tips on this one?

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Write data from ListBox to File - exclude <CR><LF> when writing

    Not sure what is it that you want - listbox is a list of some text items where each appears on a new line (so they are called items).
    If you want to output each item to a file as they appear in the list then file will have new line character between each item.
    Btw, you don't have to select listitem - use your loop counter:
    Code:
        Open "C:\LogFiles\" + TxtBxFileName.text For Append As #1
            For i = 0 To ListData.ListCount - 1
                Print #1, ListData.List(i)
            Next
        Close #1

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    16

    Re: Write data from ListBox to File - exclude <CR><LF> when writing

    I understand it didn't seem quite logic why I wanted to do this, so here's a little more info:

    This is regarding to my post at: http://www.vbforums.com/showthread.php?t=570684

    As my callback function is causing some wierd problems, I decided to let the ListBox store my incoming data, and then aquire the data from the ListBox and write them to a file. My incoming data is GPRS data which actually come from "large" files (> 480bytes) that are split into pieces of 480bytes per package, as the unit that transmit the GPRS-data cannot tranmit more than 480bytes per package.

    This data is received in my application, and I need to merge the files together again, the way they were in the beginning.

    So this is why my ListBox is causing problems, when I write the lines to a .txt-file, and in the middle of the sentence there appears a <CR> <LF> when there should be none.

    I understand if you get confused from all of this, but thanks anyway

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: Write data from ListBox to File - exclude <CR><LF> when writing

    Quote Originally Posted by Cato.M View Post
    ...and in the middle of the sentence there appears a <CR> <LF> when there should be none.
    Try replacing it with the space:

    Print #1, Replace(Replace(ListData.List(i), vbCr, Space(1)), vbLf, Space(1))

    or try this instead:

    Print #1, Replace(ListData.List(i), vbNewLine, Space(1))

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Write data from ListBox to File - exclude <CR><LF> when writing

    It seems like this function appends a <CR> and <LF> (carriage return+line feed) to the end of each line it writes from the ListBox. I do not want this. I want to write the data from the ListBox exactly as it is, line by line without inserting any <cr> nor <lf>.
    Assuming the ListBox data does not contain any new line characters.

    You need to specify the charpos argument to the Print statment. The charpos determines the position where the next Print statment will begin. If omitted printing begins on the next line.

    Use a semi-colon and the next Print statement begins at the position immediately after the data from a previous print statement.

    The Spc statement can be used to add a space between the listbox data.

    Print #1, ListData.List(i);Spc(1);
    Last edited by brucevde; May 27th, 2009 at 02:32 PM.

  6. #6
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,355

    Re: Write data from ListBox to File - exclude <CR><LF> when writing

    Build a form with a list box and two command buttons. Then apply this code.
    Code:
    Const ListSize = 20
    Private Sub Command1_Click()
    ' Write List Box to File
    Open "MyDataFile" For Output As #1
    For I = 1 To List1.ListCount
        Print #1, List1.List(I - 1); Chr$(32);
    Next
    Close
    End Sub
    
    Private Sub Command2_Click()
    ' Read File and Build List Box with Contents
    Dim MyData As String, ArrItem() As String
    List1.Clear
    Open "MyDataFile" For Input As #1
    MyData = Input(LOF(1), #1)
    ArrItem = Split(MyData, Chr$(32))
    For I = 0 To UBound(ArrItem) - 1
        List1.AddItem ArrItem(I)
    Next
    Close
    MsgBox "List Box has " & List1.ListCount & " items."
    End Sub
    
    Private Sub Form_Load()
    ' Build List Box
    For I = 1 To ListSize
        List1.AddItem I
    Next
    End Sub
    Doctor Ed

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    16

    Re: Write data from ListBox to File - exclude <CR><LF> when writing

    Thanks guys,
    I was not aware of the ";" functionality to the Print-function.
    Well, problem solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width