Open & Save Functions (VB2005)

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

Open & Save Functions (VB2005)

Post by PlasmaGhost »

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.

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

Code: Select all

data = LoadFile("FileType (*.txt)|*.txt", "Open Text File", False)
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

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

Code: Select all

SaveFile(data, "FileType (*.txt)|*.txt", "Save Text File")
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.

Code: Select all

filename = SaveFile(data, "FileType (*.txt)|*.txt", "Save Text File")
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*
Onetoomanysodas





Posts: 325
Joined: Mon Mar 22, 2004 3:59 pm
Location: Everywhere but nowhere
Contact:

Post by Onetoomanysodas »

Hmmm, why does this look familiar, maybe because it's not your code? http://cespage.com/vb/vbdntut5.html
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

Post by PlasmaGhost »

Onetoomanysodas wrote:Hmmm, why does this look familiar, maybe because it's not your code? http://cespage.com/vb/vbdntut5.html
I never said I made it by myself, I said I wrote the functions, not the code.
Hey man, w/e I'll just rewrite it. It's not like I don't know exactly what that does lolz.
xray15





Posts: 26
Joined: Fri Apr 01, 2005 6:51 pm

Post by xray15 »

Why do halo mod members like to mess with people
Hmmm, why does this look familiar, maybe because it's not your code? http://cespage.com/vb/vbdntut5.html
I hate when people do that! :evil:
ImageImageImageImageImageImage
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

Post by PlasmaGhost »

does anyone like the code?
User avatar
Phenomena





Posts: 1510
Joined: Thu Jul 01, 2004 3:25 pm
Location: Training my ducks
Contact:

Post by Phenomena »

xray15 wrote:Why do halo mod members like to mess with people
Hmmm, why does this look familiar, maybe because it's not your code? http://cespage.com/vb/vbdntut5.html
I hate when people do that! :evil:
its a small thing called forgery, copy-right infringment... whatever you want to call it/
Image
And if they don't accept Jesus as their Personal Savior, you can kill them later. How cool is that!
Post Reply