This was working

PureSc0pe

Well-known member
Joined
Mar 17, 2004
Messages
161
Location
Arizona
Last time I used this it was working. Not sure what couldve happened to it.
All it needs to do is add to the end of the text file what is typed in the text file.

VB Code:

Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim line As String = TextBox3.Text
        Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder
        Do
            If line <> TextBox3.Text Then
                SB.Append(line)
                SB.Append(System.Environment.NewLine)
            End If
        Loop Until line = TextBox3.Text
        Dim SW As New IO.StreamWriter(TextBox2.Text, True)

        SW.Write(SB.ToString())
        SW.Close()
End Sub
[edit]I added in Visual Basic markup tags. -Derek[/edit]
 
Last edited by a moderator:
Code:
Dim line As String = TextBox3.Text
Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder
Do
If line <> TextBox3.Text Then

Heuuu... line = TextBox3.Text
And line <> TextBox3.Text....
Itll never gonna get inside the IF
lollllll
Hell do the loop however !!
 
I think youre missing a StreamReader in there somewhere... as I recall, you opened the StreamReader before the Do Loop, called the .ReadLine from inside the loop and assigned it to line, and then closed the StreamReader after the loop ended (before the StreamWriter).
 
Iceplug said:
I think youre missing a StreamReader in there somewhere... as I recall, you opened the StreamReader before the Do Loop, called the .ReadLine from inside the loop and assigned it to line, and then closed the StreamReader after the loop ended (before the StreamWriter).

Am glad youre helpful, unlike Arch4ngel who just says the code is no good. Anyways, here is what I got out of what you said. The part where you said add sr.readline before and inside the do loop confused me though. What do I have to do to make this work?

Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim line As String = TextBox3.Text
        Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder
        Dim SR As System.IO.StreamReader
        Do
            SR.ReadLine()
            If line <> (TextBox3.Text) Then
                SB.Append(line)
                SB.Append(System.Environment.NewLine)
            End If
        Loop Until line = TextBox3.Text
        SR.Close()
        Dim SW As New IO.StreamWriter(TextBox2.Text, True)

        SW.Write((SB.ToString()))
        SW.Close()
End Sub
[edit]I added in Visual Basic markup tags. -Derek[/edit]
 
Last edited by a moderator:
I notice you are declaring your streamreader but not actually opening in, you will need to set SR = new System.IO.StreamReader(<file path here>). Also do you want to stop looping when the text equals the contents of textbox3 or when the file has no more data? Also when you issue the SR.ReadLine command you are not assigning the result to anything.

Have a look at the following and see if it helps
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim line As String = TextBox3.Text
        Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder()
        Dim SR As System.IO.StreamReader
        SR = New System.IO.StreamReader(TextBox1.Text)
        Do
            line = SR.ReadLine()
            If line <> (TextBox3.Text) Then
                SB.Append(line)
                SB.Append(System.Environment.NewLine)
            End If
        Loop Until line Is Nothing
        SR.Close()
        Dim SW As New IO.StreamWriter(TextBox2.Text, True)

        SW.Write((SB.ToString()))
        SW.Close()
    End Sub
 
PlausiblyDamp said:
SR = New System.IO.StreamReader(TextBox1.Text)

No matter what Ive tried with that, it wont work. Why is this? I just want the text from the textbox to be added to the end of the file. If I use TextBox1.Text, it shows no errors but doesnt work and the debug shows that line at fault and if I put the file location in instead it says the file is not declared.

Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim line As String = TextBox3.Text
        Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder
        Dim SR As System.IO.StreamReader
        SR = New System.IO.StreamReader(TextBox1.Text)

        Do
            line = SR.ReadLine()
            If line <> (TextBox3.Text) Then
                SB.Append(line)
                SB.Append(System.Environment.NewLine)
            End If
        Loop Until line Is Nothing
        SR.Close()
        Dim SW As New IO.StreamWriter(TextBox2.Text, True)

        SW.Write((SB.ToString()))
        SW.Close()
    End Sub
 
Last edited by a moderator:
The code that you have there is for deleting the line that you have typed in textbox from the end of the file.
For appending to the end of the file, all you need to do is open the StreamWriter and then .WriteLine(TextBox3.Text) to write to the end.
None of that StringBuilder and StreamReader stuff. :)

(I thought you were still doing the deleting from file... sorry.)
 
Iceplug said:
The code that you have there is for deleting the line that you have typed in textbox from the end of the file.
For appending to the end of the file, all you need to do is open the StreamWriter and then .WriteLine(TextBox3.Text) to write to the end.
None of that StringBuilder and StreamReader stuff. :)

(I thought you were still doing the deleting from file... sorry.)


Wow, that was really confusing...However it does now work!
 
Back
Top