SaveDialog and OpenDialog in VB.net

Ruud

Member
Joined
Jan 2, 2007
Messages
10
Programming Experience
1-3
hello everyone,
how use SaveDialog and OpenDialog to save and Open file in VB.net.
for example, if i want to save value of my variable in specific file.
total=10
max=4, etc
and how i can save it in file, so i can load that value by open the OpenDialog??
please help me! Thanks you
 
The OpenFileDialog and SaveFileDialog provide an interface for the user to select a file path. That path is usually, but not always, used to open the selected file or save to the selected file. They are both generally used like this:
VB.NET:
If myFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
    'The user selected a file path and pressed the OK button.
End If
You would perfrom the appropriate operations inside that If block.

Inside the If block you have two main members of interest. The FileName property returns the full path of the selected file. If you were deleting the selected file it would look like this:
VB.NET:
IO.File.Delete(myFileDialog.FileName)
You can also open the selected file by creating a StreamReader using the path or whatever is appropriate, e.g.
VB.NET:
Dim sr As New IO.StreamReader(myFileDialog.FileName)
The other member of interest is the OpenFile method. In both cases this returns a FileStream on the selected path, which is an object that gives you access to the bytes that make up the file. In the case of the OpenFileDialog the FileStream is read-only. You can use that FileStream in whatever way is appropriate. For instance, it can also be used to create a StreamReader:
VB.NET:
Dim sr As New IO.StreamReader(myFileDialog.OpenFile())
Now, you need to determine in exactly which format you want to save your data. If it's as text then a StreamReader/StreamWriter is the appropriate type to use to read and write the file. I suggest that you read about those classes in the MSDN library. You will learn a lot from the documentation itself, plus it links to topics specifically dedicated to reading and writing text files, amongst other things.
 
How to Saving value of variables into a file?

i want to do these...
How to Saving value of variables into a file?
case:
'on module
VB.NET:
Module Module1
Public x, y, z as Integer 
Public a, b, c as String
End Module
'on Form
there are 6 Textbox: T1, T2, T3,...T6; And 3 Button: Button1, Button2, Button3;
'code for button1
VB.NET:
Private Sub Button1_Click(...) Handles Button1.Click
 
x=me.T1.Text
y=me.T2.Text
z=me.T3.Text
a=me.T4.Text
b=me.T4.Text
c=me.T4.Text
End Sub
for example: now the values of these variables are:
x=1
y=2
z=3
a="Test1"
b="Test2"
c="Test3"
so, when i click Button2, i want to save all of these variables value into a file(file1.txt),
and i close my aplication and then run it again. my variables now empty(x,y,z,a,b,c)
so when i click button3, i want to load the selected file(file1.txt) who saving the values of variables into my variables;
so when i need these value, i don't have to type the value of each variables on my Textboxs again to fill these variables.
note: the value of variables(x,y,z,z,b,c) not only one, we can save new variables value into a new file, so we can load it again when we need it. it's meant that we may having much file which it saving our variables value.

what must i do?
somebody have an Idea to do that? Please help me!
Thanks very Much Before..
 
Last edited by a moderator:
I've already provided some help, so you should be able to at least partially write the code. Have you followed any of the suggestions in my previous post? Have you read about the StreamReader and StreamWriter classes in the MSDN library? Have you followed the links in those help topics that lead to topics like "How to: Write Text to a File"? The information you need is at your fingertips so I would think that a reasonable attempt at the code is not beyond you at this point. I wouldn't expect it to be perfect but if you are waiting for someone to simply provide all the code for you then I'm afraid you'll have to wait for someone else.
 
VB.NET:
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim StreamWriter1 As System.IO.StreamWriter
            StreamWriter1 = My.Computer.FileSystem.OpenTextFileWriter(SaveFileDialog1.FileName, True)
            StreamWriter1.WriteLine(T1.Text)
StreamWriter1.Close()

You of course have to add a save file dialog to your project and add other variables as you need them I did one for you. And I believe you need to include

Imports System
Imports System.IO

VB.NET:
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim StreamReader1 As System.IO.StreamReader
            StreamReader1 = My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
            T1.Text = StreamReader1.ReadLine()
StreamReader1.Close()

You also need a open file dialog again you can find that in the toolbox if your using vs 2005 I dont know about any of the other compilers. But there you have it again I added the same example. Also their is no need to declare seprate variables for your text boxes unless you plan on changing types as in going from a string to an interger if not then the simple T1.Text will do to tell the writer/reader where to read/write to.
 
hello

hello, thanks for the solutions. i think that, i had knew the solutions for my problem now. thanks for the suggestions too.
 
Back
Top