Open & Save Functions (VB2005)
Posted: Wed Oct 19, 2005 5:47 pm
Ok people, i wrote 2 functions to help newbies and not so newbies write and load text to/from files. ok so, you need to put this code below the Public Class section but above all Subs. So, here they are.
ok you dont need to know ANYTHING about that long piece of code. that now, to actually load the files. lets say you are doing a load for button1 if the user clicks it. heres the code and i will break it down.
Now, here's the breakdown. switch data with whatever you want this to write the text it reads to. so like TextBox1.Text you could put there. Leave the LoadFile( part alone. The "FileType (*.txt)|*.txt" is the filter just like an OpenFileDialog is. the "Open Text File" is the dialog title. The last value can be either True or False. That is the Multiselect option which allows you to select multiple files. ok now the save file function
ok now, this is a little different. this can write to files AND give you filename at the same time. but first i will show you how to just write to files. do this code
so, heres the break down. switch data with what you want to get the data to write the file from (if data = hi!, then the function will write hi! to the file). The filetype is the same as i said above. The "Save Text File" is the Dialog Title. ok now to have it write the filename also? do this.
same this just swap filename with what you want it to write the filename to.
I know, there are a few problems like no init directory and maybe more, but i am working on them. i would fix it but i am very busy on my latest project, HMMN, which uses these function plus other functions i wrote. check it out when its done!
ok like usual, if you need more help, if theres any problems with the code, just PM me, AIM me, Email me, or just leave a message in this forum. thanks everyone.
*mods please sticky, lots of people seem to have problems opening/saving files, thanks*
Code: Select all
Private Function LoadFile( _
ByVal Filter1 As String, _
Optional ByVal DialogTitle As String = "Open", _
Optional ByVal MultiSelect As Boolean = False, _
Optional ByVal ErrorMsg As String = "An error has occured." _
) As String
Dim Open As New OpenFileDialog()
Dim myStreamReader As System.IO.StreamReader
Dim text As String = ""
Dim Filter2 As String = Filter1
Dim DialogTitle2 As String = DialogTitle
Dim EMsg As String = ErrorMsg
Dim Multi As Boolean = MultiSelect
Open.Filter = Filter2
Open.CheckFileExists = True
Open.Title = DialogTitle2
Open.Multiselect = Multi
Open.ShowDialog(Me)
Try
Open.OpenFile()
myStreamReader = System.IO.File.OpenText(Open.FileName)
text = myStreamReader.ReadToEnd()
Catch ex As Exception
MsgBox(EMsg)
End Try
Return text
End Function
Code: Select all
data = LoadFile("FileType (*.txt)|*.txt", "Open Text File", False)
Code: Select all
Private Function SaveFile( _
ByVal Data As String, _
ByVal Filter1 As String, _
Optional ByVal DialogTitle As String = "Save", _
Optional ByVal ErrorMsg As String = "An error has occured." _
) As String
Dim Save As New SaveFileDialog()
Dim myStreamWriter As System.IO.StreamWriter
Dim Data2 As String = Data
Dim Filter2 As String = Filter1
Dim DialogTitle2 As String = DialogTitle
Dim EMsg As String = ErrorMsg
Save.Filter = Filter2
Save.CheckPathExists = True
Save.Title = DialogTitle2
Save.ShowDialog(Me)
Try
myStreamWriter = System.IO.File.CreateText(Save.FileName)
myStreamWriter.Write(Data2)
myStreamWriter.Flush()
Catch ex As Exception
MsgBox(EMsg)
End Try
Return Save.FileName
End Function
Code: Select all
SaveFile(data, "FileType (*.txt)|*.txt", "Save Text File")
Code: Select all
filename = SaveFile(data, "FileType (*.txt)|*.txt", "Save Text File")
I know, there are a few problems like no init directory and maybe more, but i am working on them. i would fix it but i am very busy on my latest project, HMMN, which uses these function plus other functions i wrote. check it out when its done!
ok like usual, if you need more help, if theres any problems with the code, just PM me, AIM me, Email me, or just leave a message in this forum. thanks everyone.
*mods please sticky, lots of people seem to have problems opening/saving files, thanks*