Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All really need is something that will take information in all text boxes and the date picker and put in on a notepad document and save it in a pre determined folder. The save should occure after hitting a button of course. (I'm kinda knew to VB)

(assume that text boxers are labled TextBox1, TextBox2, and etc. And date picker is label with the predetermined name)

I'm using VB 2010
Posted
Updated 16-Mar-12 7:07am
v2

Alternative 1: Iterate all controls of the form and write the text of all the TextBoxes to a text file
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Handles Button1.Click
    
    Dim StringBuilder1 As StringBuilder = New StringBuilder()

    StringBuilder1.Append(DateTimePicker1.Value.ToLongDateString() & vbCrLf)
    For Each Control1 As Control In Controls
        If TypeOf Control1 Is TextBox Then
            StringBuilder1.Append(DirectCast(Control1, TextBox).Text.Trim() & vbCrLf)
        End If
    Next
    'The file will be created in the Folder where the
    'executable file of this program is there.
    System.IO.File.WriteAllText(Application.StartupPath & 
            "\AllTextFile.txt", StringBuilder1.ToString())

End Sub


Alternative 2: Write the text of specified TextBoxes to a text file
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Handles Button1.Click

    Dim StringBuilder1 As StringBuilder = New StringBuilder()

    StringBuilder1.Append(DateTimePicker1.Value.ToLongDateString() & vbCrLf)
    StringBuilder1.Append(TextBox1.Text & vbcrlf)
    StringBuilder1.Append(TextBox2.Text & vbcrlf)
    StringBuilder1.Append(TextBox3.Text & vbcrlf)
    'The file will be created in the Folder where the
    'executable file of this program is there.
    System.IO.File.WriteAllText(Application.StartupPath & 
            "\AllTextFile.txt", StringBuilder1.ToString())

End Sub
 
Share this answer
 
v4
Try something like this

VB
Using writer As IO.StreamWriter = new IO.StreamWriter(IO.Path.Combine("C:\myFile.txt"), True)
            writer.WriteLine("Text1: " & TextBox1.Text)
            writer.WriteLine("Date: " & DateTimePicker.Value.ToString)
End Using
 
Share this answer
 

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