Resolved Saving text . . . . how?

Dimuthu93

Member
Joined
Oct 18, 2009
Messages
13
Programming Experience
Beginner
ok, so i have a text box with text, a button and a severe headache. How do i get the text from the text box to a (*.txt).

some help would be appreciated

some coding on how to do this would be extremely appreciated
Thanks in advance
dimuthu
 
Last edited:
i have been looking around the internet for this all morning, and i came across this term several times, wth is 'Boolean'

Dimuthu
 
A boolean is basically a true or false value.

As for writing text files...

VB.NET:
Imports System.IO

Dim writer As New StreamWriter("C:\Example.txt")

writer.WriteLine("Some line of text")
writer.WriteLine("Another line of text")

writer.Close()

That would create a text file called Example.txt with two lines.
The Imports System.IO line needs to go outside your class.
 
yes i have seen this before but hpw could you make it so that the user can decide the name of the txt file and the location? you know the dialogue box in most microsoft products?
thx mate
Dimuthu
 
You could use a save file dialog to allow the user to type in a file name, then create a new stream writer referencing the file name the user has chosen.

VB.NET:
    Private Sub WriteFile()
        Dim writer As StreamWriter
        Dim saveFileDialog As New SaveFileDialog()

        saveFileDialog.Filter = "txt files (*.txt)|*.txt"

        If saveFileDialog.ShowDialog() = DialogResult.OK Then
            writer = New StreamWriter(saveFileDialog.OpenFile())
            If (writer IsNot Nothing) Then
                'Save the text from your text box here
                writer.WriteLine("Hi!")
                writer.Close()
            End If
        End If
    End Sub

Just use a button to call the sub.
 
hoho,
there are 2 problems which i encounter when i use the code you have typed;
1 Streamwriter is underlind and it states, "Type Streamwriter is not defined"
2 I do not understand what you mean by "Just use a button to call the sub."
sorry

yea i know i can be a little dumb sometimes
 
1 - You didn't add "Imports System.IO" to the top of the file.
2 - Just double-click the button you want to use to call the code, then add:

VB.NET:
    WriteFile()

to the code that pops up. That will call the sub "WriteFile" shown in the previous post, and the code contained within, the saving code, will be called.
 
Back
Top