ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

38-OpenFileDialog

The OpenFileDialog control allows the user to open a file and select any file to open. The user can also check if the file does exists and open it. The OpenFileDialog control class that derives from the abstract class FileDialog.

 

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 openfiledialog in the form. Do the form just like this.
    38openfile
  • Double click the button and do this following code.
  1. Try
  2. With OpenFileDialog1
  3.  
  4. 'CHECK THE SELECTED FILE IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  5. .CheckFileExists = True
  6.  
  7. 'CHECK THE SELECTED PATH IF IT EXIST OTHERWISE THE DIALOG BOX WILL DISPLAY A WARNING.
  8. .CheckPathExists = True
  9.  
  10. 'GET AND SET THE DEFAULT EXTENSION
  11. .DefaultExt = "txt"
  12.  
  13. 'RETURN THE FILE LINKED TO THE LNK FILE
  14. .DereferenceLinks = True
  15.  
  16. 'SET THE FILE NAME TO EMPTY
  17. .FileName = ""
  18.  
  19. 'FILTERING THE FILES
  20. .Filter = "Text files (*.txt)|*.txt|All files|*.*"
  21.  
  22. 'SET THIS FOR ONE FILE SELECTION ONLY.
  23. .Multiselect = False
  24.  
  25. 'SET THIS TO PUT THE CURRENT FOLDER BACK TO WHERE IT HAS STARTED.
  26. .RestoreDirectory = True
  27.  
  28. 'SET THE TITLE OF THE DIALOG BOX.
  29. .Title = "Select a file to open"
  30.  
  31. 'ACCEPT ONLY THE VALID WIN32 FILE NAMES.
  32. .ValidateNames = True
  33.  
  34. If .ShowDialog = Windows.Forms.DialogResult.OK Then
  35. Try
  36. RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(.FileName)
  37. Catch fileException As Exception
  38. Throw fileException
  39. End Try
  40. End If
  41.  
  42. End With
  43. Catch ex As Exception
  44. MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
  45. End Try

Output:

38-openfile1f

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!