PureSc0pe Posted March 29, 2004 Posted March 29, 2004 (edited) Last time I used this it was working. Not sure what could've 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: 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] Edited March 31, 2004 by Derek Stone Quote It's not impossible, it's Inevitable.
Arch4ngel Posted March 29, 2004 Posted March 29, 2004 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.... It'll never gonna get inside the IF lollllll He'll do the loop however !! Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
Leaders Iceplug Posted March 29, 2004 Leaders Posted March 29, 2004 I think you're 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). Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Arch4ngel Posted March 29, 2004 Posted March 29, 2004 What a buggy code isn't it ? Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
PureSc0pe Posted March 30, 2004 Author Posted March 30, 2004 What a buggy code isn't it ? If it worked, I wouldn't be here. Try thinking before typing. Quote It's not impossible, it's Inevitable.
PureSc0pe Posted March 30, 2004 Author Posted March 30, 2004 (edited) I think you're missing a StreamReader in there somewhere... as I recall' date=' 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).[/quote'] Am glad you're 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? 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] Edited March 31, 2004 by Derek Stone Quote It's not impossible, it's Inevitable.
Administrators PlausiblyDamp Posted March 30, 2004 Administrators Posted March 30, 2004 I notice you are declaring your streamreader but not actually opening in, you will need to set SR = new System.IO.StreamReader(). 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 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
PureSc0pe Posted March 31, 2004 Author Posted March 31, 2004 (edited) SR = New System.IO.StreamReader(TextBox1.Text) No matter what I've tried with that, it won't 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 doesn't 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. 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 Edited March 31, 2004 by PureSc0pe Quote It's not impossible, it's Inevitable.
Administrators PlausiblyDamp Posted March 31, 2004 Administrators Posted March 31, 2004 So TextBox1 contains text to be appended to the file? If so which textbox contains the file name to be opened? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders Iceplug Posted March 31, 2004 Leaders Posted March 31, 2004 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.) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PureSc0pe Posted April 1, 2004 Author Posted April 1, 2004 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! Quote It's not impossible, it's Inevitable.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.