| | | Junior Member
       
Group: Forum Members Last Login: 11/30/2007 1:44:01 AM Posts: 23, Visits: 144 |
| | Hi everyone, I'm using Visual Basic 2005. What i am going to do is:
1) Whatever i typed into the textbox, once i click the button, it will automatic save into a file which is in .txt form 2) Do not overwrite the file, just add into it. I have read through a method of Saving a File from a book:
Imports System.IO Public Class Form1 Dim myFileContents As String
Private Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click
If SaveFileDialog1.ShowDialog() = DialogResult.OK Then Dim myStreamWriter As New StreamWriter(SaveFileDialog1.FileName) If Not (myStreamWriter Is Nothing) Then myStreamWriter.Write(FileContents.Text) myStreamWriter.Close() End If End If End Sub End Class
- I have tried the program above, but it cannot auto save in text form. Everytime i save a file, i need to type "abc.txt".
- And it only can either save into another file or overwrite it.
Q1: How to let it auto save in a text file? Q2: And is there any method which no need to rename the file and just let it ADD into the existing file (not overwrite) Thank you 
Regards
Wanxi |
| | | | 
Forum God
       
Group: Forum Members Last Login: Today @ 1:10:14 PM Posts: 1,502, Visits: 3,491 |
| | Welcome to A1vbcode Wanxi. Well I can't speak for Visual Basic 2005 but in VB6 you can use Open App.Path & "\abc.txt" For Append As #1 Print #1, FileContents.Text Close #1 You will have to be careful that the text doesn't get to big because I think there is a limit an the filesize on the txt file. Append will add text to an extisting file without overwriting it. If the file doesn't exist then it will create a new one.  Does VB 2005 have a descent help? Do a search for Input, Output and Append.
Keith http://www.martin2k.co.uk/forums/ I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning. |
| | | | Junior Member
       
Group: Forum Members Last Login: 11/30/2007 1:44:01 AM Posts: 23, Visits: 144 |
| Thank You the information from Anonymous and LadzOfSoul  Below are the 2 answers that i received by private message from them. I have study both of them, there is not much different at the last part, except for: "myStreamWriter.Write(ans & TextBox1.Text)" By using a WriteLine for me will be better, bcos i'm going to receive a same data but different time. Another things is "OpenFileDialog1" If you are not required to keep rename the file's name then you no need to put OpenFileDialog, just type the address at the Stream. 'ANSWER of Anonymous : Imports System.IO Public Class Ans1Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim ans As String 'ans is the File Contents Dim myStreamReader As New StreamReader("c:\New Text.txt") 'Open & read from the location If Not (myStreamReader Is Nothing) Then 'if myStreamReader is not Nothing? (means there is a StreamReader?) ans = myStreamReader.ReadToEnd.ToString 'Read the New Text and put it into ans myStreamReader.Close() 'After read must close Dim myStreamWriter As New StreamWriter("c:\New Text.txt") 'Write to the location myStreamWriter.WriteLine(ans & TextBox1.Text) 'Write the ans that i read from the file and add the words that i type into the TextBox1 in a new line myStreamWriter.Close() 'After write also need to close End If End Sub End Class
'ANSWER of LadzOfSoul :Imports System.IO Public Class Ans2Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ans As String If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then Dim myStreamReader As New StreamReader(OpenFileDialog1.FileName) If Not (myStreamReader Is Nothing) Then ans = myStreamReader.ReadToEnd.ToString myStreamReader.Close() Dim myStreamWriter As New StreamWriter(OpenFileDialog1.FileName) myStreamWriter.Write(ans & TextBox1.Text) myStreamWriter.Close() End If End If End Sub End Class
There are some problems that i wonder: - What is the meaning of "If Not (myStreamReader Is Nothing) Then"?
- And for this >> "Dim myStreamReader As New StreamReader(OpenFileDialog1.FileName)" Ist "OpenFileDialog1.FileName" is a fixed name? Or what else i can put on it?
I never use myStreamReader/Writer before, so there is a bit difficult for me to understand a single program, by compare them make me realies how it works.. Thanks alot.. 
Regards
Wanxi |
| | | | 
Forum God
       
Group: Forum Members Last Login: Today @ 5:04:51 PM Posts: 866, Visits: 5,185 |
| | This line of code: If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
You are calling the save dialog everytime you save the file. Somewhere else in your code put: Dim FileName As String = String.Empty If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then FileName = SaveFileDialog1.FileName End If
and change to this: Dim myStreamWriter As New StreamWriter(FileName)
WayneI know enough to be dangerious. It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living. |
| |
|
|