Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by Derek Stone
It's not impossible, it's Inevitable.
Posted

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 !!

"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
Posted
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).

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
What a buggy code isn't it ?

"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

Posted (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 by Derek Stone
It's not impossible, it's Inevitable.
  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (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 by PureSc0pe
It's not impossible, it's Inevitable.
  • Leaders
Posted

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, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
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!

It's not impossible, it's Inevitable.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...