VB Tutorial
Tools
Free Downloads
Free Link Directory

Basics
Data Types
Windows Forms
Buttons
Check Box
Text Box
Combo Box
Radio Buttons
Progress Bar
Color & Font Dialogs
Open & Save Dialogs
Process Control

More Complex Examples
Audio Player
Rotating Rectangle


 

Basic controls with Visual Basic 2008

Open and Save File Dialogs

OpenFileDialogs:
OpenFileDialogs are used to browse your computer, select a file and open it. You can also set the Filter attribute to allow only certain file extensions. FileDialogs are usually called with the ShowDialog command.

SaveFileDialogs:
SaveFileDialogs are also used to browse your computer, select a file and save it. If you ever used the Save As function of some application, you would know exactly what these dialogs look like.
We will build a simple example to give you an idea of how these dialogs work.
Create a new project. From the Toolbox menu create the following controls on the form:

OpenFileDialog1
SaveFileDialog1
Label1
Label2
TextBox1
TextBox2
TextBox3
TextBox4
TextBox5
TextBox6
TextBox8
Button1
Button2

Arrange the controls as in figure 9:


Figure 9: Open & Save Dialogs

Change the text of Button1 to "Open" and Button2 to "Save". Change the text of Label1 to "Name" and Label2 to "Age". Create a String array called "Names" to hold 4 data entries and an Integer array called "Age" to hold 4 data entries using the "Dim" command. Use Button1 with the FileOpenDialog, ShowDialog and Button2 with the FileSaveDialog, ShowDialog command.

Public Class Form1
Dim Names(3) As String, Age(3) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "Data files (*.dat)|*.dat"
OpenFileDialog1.ShowDialog()
FileOpen(1, OpenFileDialog1.FileName, OpenMode.Random)
FileGet(1, Names) : FileGet(1, Age)
TextBox1.Text = Names(0)
TextBox2.Text = Names(1)
TextBox3.Text = Names(2)
TextBox4.Text = Names(3)
TextBox5.Text = Age(0)
TextBox6.Text = Age(1)
TextBox7.Text = Age(2)
TextBox8.Text = Age(3)
FileClose(1)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SaveFileDialog1.Filter = "Data files (*.dat)|*.dat"
SaveFileDialog1.ShowDialog()
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Random)
Names(0) = TextBox1.Text
Names(1) = TextBox2.Text
Names(2) = TextBox3.Text
Names(3) = TextBox4.Text
Age(0) = TextBox5.Text
Age(1) = TextBox6.Text
Age(2) = TextBox7.Text
Age(3) = TextBox8.Text
FilePut(1, Names) : FilePut(1, Age)
FileClose(1)
End Sub
End Class

We added the Filter "Data files (*.dat)|*.dat" to both FileDialogs so that when we try to open a file only files with the .dat extension can be showed. When we save our data to a file the .dat extension will automatically be added to our filename. Neat don't you think. Our FileDialogs does not actually open or save our file, but in fact only gives us a browsing tool to select a path and filename. We use the FileOpen command to open a file. Files can be opened in different modes to which I will give you more information later on, but for now lets stick to OpenMode.Random. You will notice that after both FileOpen and FileClose methods we use  the number (1). It is to specify the file number. To either write to or read from a file we use the commands FilePut or FileGet with the file number.
If you liked this article then Please Thumb This Up with a short review - Stumble It!
Continue to the next page.

 


Email us at: info@pro2visual.com
 

Get your Website Listed on our Free Link Banner Directory
Advertise your site with your own unique link banner we will also provide text links to your site.

Popular Articles

Moving a borderless form in vb.net

ComboBox1.Items.Add

Link Progress Bar with Timer in vb.net

How to open a folder using the process control

Start Vb.net programming. No get rich quick scam

How to Convert the Date Format

Click here for Industry Leading Development Softwa