Results 1 to 6 of 6

Thread: Save text boxes as file

  1. #1

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Save text boxes as file

    Hey,

    I do apologise for a simple post, but am finally making the transition from vb6 to .NET. I used the following code to save a series of text boxes to a file in vb6. I basially need the same code for VB.NET

    Code:
    Dim sFileText as String
    Dim iFileNo as Integer
    iFileNo = FreeFile
    Open "C:\Test.txt" For Output As #iFileNo
    
    'Write data
    Print #iFileNo, tb1.text
    Print #iFileNo, tb2.text
    Print #iFileNo, label.caption
    Print #iFileNo, "etc."
    Close #iFileNo
    Regards,

    Adam.

  2. #2
    Member BlueCharge's Avatar
    Join Date
    Aug 2009
    Posts
    61

    Re: Save text boxes as file

    Hi Adam,

    Here's what you want:

    Code:
    'Write to text file with the Stream Writer Class
    
        
    Dim sFileText as String
    Dim i as Integer
    Dim sWriter As IO.StreamWriter = New IO.StreamWriter("C:\Test.txt")
    
    sWriter.WriteLine(i & tb1.text)
    i = i + 1
    sWriter.WriteLine(i & tb2.text)
    i = i + 1
    sWriter.WriteLine(i & label.text)
    i = i + 1
    sWriter.WriteLine(i & "etc.")
    
    'Flush all data to the file.  
    
    sWriter.Flush()
    Last edited by BlueCharge; May 15th, 2010 at 07:17 AM.
    Visual Basic 2008 Express Edition...
    Level: Slightly Above Beginner...

  3. #3

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Save text boxes as file

    Thanks for this. Just starting to test it now. Is it a similar code to loading a file up, and loading each line into text boxes?

    Also, how do I go about creating a file? Have got " Dim fso As New FileIO.FileSystem " but there is no option for "create file".

    Adam.

  4. #4
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Save text boxes as file

    look at his code

    Code:
    Dim sWriter As IO.StreamWriter = New IO.StreamWriter("C:\Test.txt")
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  5. #5

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Save text boxes as file

    After reading your reply, I decided that I had the ultimate blonde moment. It was a directory that needed creating, not a file. This has been sorted now. I also looked into vb classes to reverse the code that I was given, in which I came across Streamreader. I think I am almost there with it.

    Code:
    Try
                ' Create an instance of StreamReader to read from a file.
                Dim sr As StreamReader = New StreamReader(DirectCast(ComboBox1.SelectedItem, listItem).fullPath & "/details.adf")
                Dim line As String
                ' Read and display the lines from the file until the end 
                ' of the file is reached.
                Do
                    'line = sr.ReadLine()
                    Console.WriteLine(line)
                Loop Until line Is Nothing
                sr.Close()
            Catch Exc As Exception
                ' Let the user know what went wrong.
                Console.WriteLine("The file could not be read:")
                Console.WriteLine(Exc.Message)
            End Try
    Now obviosuly that code writes each individulal line to the console. The issue I have here is there is no way to specify where the lines go.

    I basically need to say:

    line 1 - go to xx.text
    line 2 - go to yy.text

    I'm wondering how possible it would be to maybe have xx(1).text and xx(2).text. Then do it that way?

  6. #6

    Thread Starter
    Addicted Member adamlonsdale's Avatar
    Join Date
    Oct 2005
    Posts
    210

    Re: Save text boxes as file

    Probably a terrible way of doing this. But I've looped the code so it adds all items into a listbox. Then have done the following:

    Code:
            With variables
                MsgBox(.ListView1.Items.Item(0))
                MsgBox(.ListView1.Items(0))
                .clubname.Text = .ListView1.Items.Item(0).ToString
                .address1.Text = .ListView1.Items.Item(1).ToString
                .city.Text = .ListView1.Items.Item(2).ToString
                .postcode.Text = .ListView1.Items.Item(3).ToString
                .telephone.Text = .ListView1.Items.Item(4).ToString
                .email.Text = .ListView1.Items.Item(5).ToString
                .clubcontact.Text = .ListView1.Items.Item(6).ToString
                .teamanager.Text = .ListView1.Items.Item(7).ToString
                .membershipcontact.Text = .ListView1.Items.Item(8).ToString
                .ListView1.Items.Clear()
            End With

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