39-SaveFileDialog

0
308

This Visual Basic Tutorial is all about SaveFileDialog.
The SaveFileDialog component prompt the users to browse the file system and select a specific file to be saved. The dialog box returns the path and name of the file that the user has selected. On the other hand, you need to write the code in order to write the files to the disk.

 

Example :

  • Open the Visual Basic, select “File” on the menu, hit new and create a new project.
    01_helloworld
  • The New Project dialog will appear.
    create2s
    Select “windows” in the project types, hit the “windows form application” in the templates and hit “ok“.
  • Add a richbox, a button and the savefiledialogin the form. Do the form just like this.
    sfileDF1
  • Double click a button and do this following code.
  1. Try
  2. With SaveFileDialog1
  3. 'IF THE USER NEGLECTS THE FILE ETENSION THEN, ADD THE DEFUALT EXTENSION.
  4. .AddExtension = True
  5. 'CHECK IF THE OUTPUT PATH ACTUALLY EXISTS
  6. 'BEFORE CREATING A NEW FILE AND BEFORE OVERWRITING.
  7. 'THE FOLLOWING VALUES ARE IN ITS DEFUALT FORM.
  8. .CheckPathExists = True
  9. .CreatePrompt = False
  10. .OverwritePrompt = True
  11. .ValidateNames = True
  12.  
  13. 'GET AND SET THE DEFAULT EXTENSION
  14. .DefaultExt = "txt"
  15.  
  16. 'FILLTERING THE FILES THAT YOU HAVE SAVED.
  17. .Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
  18. .FilterIndex = 1
  19.  
  20. If .ShowDialog() = Windows.Forms.DialogResult.OK Then
  21. My.Computer.FileSystem.WriteAllText(.FileName, RichTextBox1.Text, False)
  22. End If
  23.  
  24. End With
  25. Catch ex As Exception
  26. MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  27. End Try

Output:
sfileDF2

 

Reader might read also:

 

 

 

Facebook Comments
(Visited 23 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here