Saving windows applications

Tnnspro77

New member
Joined
Mar 18, 2006
Messages
3
Programming Experience
Beginner
Hey guys, heres an easy question, so im fairly new to vb.net and ive been writing some programs, simple ones memo reminder etc. So I want to allow the user to save and open the same memo whenever the program is used.
How do I do this? Ive added a save dialog box and a open dialog box but I can't seem to figure the code to allow them to do what I want.
So this is a stupid question but can someone help me with the code or point me in the right direction.
-Thanks.
 
in what format are the memo's going to be saved, it's easy if they're plain text or RTF

for plain text simply use System.IO.StreamReader and System.IO.StreamWriter

for RTF the RichTextBox control has a SaveFile() and LoadFile() methods for saving and reading
 
The Easiest Way

The easiest way to save files is using System.IO namespace
VB.NET:
[/SIZE][/FONT]
 
[FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] SaveTextFile()[/FONT]
[FONT=Courier New][COLOR=blue]Dim[/COLOR] fileDialog [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] System.Windows.Forms.SaveFileDialog[/FONT]
 
[FONT=Courier New]fileDialog.Filter = [COLOR=maroon]"Memo files (*.memo)|*.memo|All files (*.*)|*.*"[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]If[/COLOR] fileDialog.ShowDialog = Windows.Forms.DialogResult.OK [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] System.IO.Directory.GetParent(fileDialog.FileName).Exists [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New]System.IO.Directory.GetParent(fileDialog.FileName).Create()[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT=Courier New]System.IO.File.WriteAllText(fileDialog.FileName, MyMemoTextSource)[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] SaveBinaryFile()[/FONT]
[FONT=Courier New][COLOR=blue]Dim[/COLOR] fileDialog [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] System.Windows.Forms.SaveFileDialog[/FONT]
 
[FONT=Courier New]fileDialog.Filter = [COLOR=maroon]"Memo files (*.memo)|*.memo|All files (*.*)|*.*"[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]If[/COLOR] fileDialog.ShowDialog = Windows.Forms.DialogResult.OK [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] System.IO.Directory.GetParent(fileDialog.FileName).Exists [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New]System.IO.Directory.GetParent(fileDialog.FileName).Create()[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT=Courier New]System.IO.File.WriteAllBytes(fileDialog.FileName, MyMemoByteSource())[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] OpenTextFile()[/FONT]
[FONT=Courier New][COLOR=blue]Dim[/COLOR] fileDialog [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] System.Windows.Forms.SaveFileDialog[/FONT]
 
[FONT=Courier New]fileDialog.Filter = [COLOR=maroon]"Memo files (*.memo)|*.memo|All files (*.*)|*.*"[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]If[/COLOR] fileDialog.ShowDialog = Windows.Forms.DialogResult.OK [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] System.IO.File.Exists(fileDialog.FileName) [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New]MessageBox.Show([COLOR=maroon]"Ops Not File Found"[/COLOR], [COLOR=maroon]"No File Found"[/COLOR], MessageBoxButtons.OK, MessageBoxIcon.Error)[/FONT]
[FONT=Courier New][COLOR=blue]Exit[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT=Courier New]MyMemoTextSource = System.IO.File.ReadAllText(fileDialog.FileName)[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] OpenBinaryFile()[/FONT]
[FONT=Courier New][COLOR=blue]Dim[/COLOR] fileDialog [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] System.Windows.Forms.SaveFileDialog[/FONT]
 
[FONT=Courier New]fileDialog.Filter = [COLOR=maroon]"Memo files (*.memo)|*.memo|All files (*.*)|*.*"[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]If[/COLOR] fileDialog.ShowDialog = Windows.Forms.DialogResult.OK [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] System.IO.File.Exists(fileDialog.FileName) [COLOR=blue]Then[/COLOR][/FONT]
[FONT=Courier New]MessageBox.Show([COLOR=maroon]"Ops Not File Found"[/COLOR], [COLOR=maroon]"No File Found"[/COLOR], MessageBoxButtons.OK, MessageBoxIcon.Error)[/FONT]
[FONT=Courier New][COLOR=blue]Exit[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT=Courier New]MyMemoByteSource() = System.IO.File.ReadAllBytes(fileDialog.FileName)[/FONT]
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
 
[FONT=Courier New][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]

[FONT=Courier New][COLOR=black]
This method you can also implement events and delegates, opening and saving via background threads, and among other things[/COLOR]
 
Last edited by a moderator:
Hey PoweredByOTGC, what were you thinking?? Tnnspro77 can't use that code in .Net 1.1.
 
Back
Top