Results 1 to 5 of 5

Thread: Multiple Textboxes to one textfile

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Multiple Textboxes to one textfile

    Hey Guys,

    at first, sorry for this noob question but im pretty new in VB^^
    So basically, i have a programm with two listboxes and i want that i can save the content of the listboxes to a txt file
    (It should be like this:
    Listbox1.Item1 + Listbox2.Item1
    Listbox1.Item2 + Listbox2.Item2 etc)

    Im currently at this point:
    Code:
    Dim sw As New System.IO.StreamWriter(SaveFileDialog1.FileName)
            Dim i As Integer
            For i = 0 To ListBox1.Items.Count - 1
                sw.WriteLine(ListBox1.Items.Item(i))
            Next
            sw.Close()
    I already tried different possibilities but never got it working
    Would be cool if you guys can help me with this

    Thanks in advance and sry for being a noob

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Multiple Textboxes to one textfile

    try this:

    Code:
    dim sfd as new SaveFileDialog
    if sfd.showdialog = dialogresult.ok then
        IO.File.WriteAllLines(sfd.FileName, Enumerable.Range(0, ListBox1.Items.Count).Select(Function(x) String.Format("{0}, {1}", ListBox1.Items(x), ListBox2.Items(x))).ToArray)
    end if

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Multiple Textboxes to one textfile

    Quote Originally Posted by .paul. View Post
    try this:

    Code:
    dim sfd as new SaveFileDialog
    if sfd.showdialog = dialogresult.ok then
        IO.File.WriteAllLines(sfd.FileName, Enumerable.Range(0, ListBox1.Items.Count).Select(Function(x) String.Format("{0}, {1}", ListBox1.Items(x), ListBox2.Items(x))).ToArray)
    end if
    Thank you very much

  4. #4
    New Member
    Join Date
    Mar 2011
    Posts
    12

    Re: Multiple Textboxes to one textfile

    .paul. he said he is new to vb man. with code like this he's gonna hate us and vb

    here is a simplified one

    Code:
            Dim s As New IO.StreamWriter("c:\path_to_file")
            For i = 0 To ListBox1.count - 1
                Dim data As String = listbox1.items(i)
                s.WriteLine(Data)
            Next
            s.Close()
    Last edited by mobarako; May 12th, 2013 at 09:59 AM.

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Multiple Textboxes to one textfile

    If the data is large enough it might even be better to avoid the LINQ. I enjoy LINQ, but there are places where a regular loop is sometimes better.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

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



Click Here to Expand Forum to Full Width