Common Dialog not only replaces three controls (Drive, Directory and FileList), but also is easier to program. It is supported in Visual Basic and VBA as well. The new VB.NET has the same functionality in the OpenFileDialog class
Let us have a small form created for explaining CommonDialog. Let us have a small form with a Text Box and a Command Button. On Clicking the Command Button, the selected file should be displayed in the Text Box
Sample Form:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigKeBF0vpsfupC9BMcgcCR6IYSXfpbU7XDCq7v8oJp22vrq6FqmQE_5mr0bxn35xLTG_-obXzzUOrKkRGuEZPLA0o3CHqcWp1Mboa-NOcPyhyphenhyphen-MhCba8XxSIDbVJyg-R8ixVAw/s400/EmptyForm.gif)
To use the CommonDialog you need to include the component to your project. You can do so as follows:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYdAdmikB1xvZvc4jMrcoPQuvIDx0dr_shyphenhyphenEytbzBTvaAdtcj1lxzQb-WkZRBxsXEfBRdeH1i_DTC_Y4a8vyzWeARDLyJzN38wTv9LV5pysd55-rWK723sJo_qM6GmAiOUlrhw/s400/CommonDialogComponent.gif)
Once The component is included, the CommonDialog will be displayed in the ToolBox
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLH3TxeDA-q5lo2b_DyH3jZlBFXtmG0n1fmCYT9rdB0HwwHgAlHFT_wT9NwZ9Y6qsQtUBgWACPAKhYFDaNm1OAx92Q2pY0RfY_LPDKz6YYtq-Lkt-DO9aFXyCJlEKIOnUMmF81/s400/CommonDialoginToolBox.gif)
Drag the CommonDialog to the form. You will see a small rectangle there. CommonDialog is visible in the Design time only (it is not visible during runtime)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9rRFVanBBUKiEL0umX5IA3RTR8sNSo5bID5mUqA1mxpRaQepAcq7Cp6a-pQTi3QdBk1ru5NPIOsL4MBZcSoIPp1BekK2ybmebvmmu-VksotakNc4OI_7BqYhm2D-wG4jBJaHJ/s400/CommonDialoginForm.gif)
Add the following code to show the CommonDialog box and show the selected file in the text box
Private Sub Command1_Click()
CommonDialog1.DialogTitle = "Select the File..."
CommonDialog1.Flags = cdlOFNFileMustExist
CommonDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls"
CommonDialog1.ShowOpen
If Len(CommonDialog1.FileName) <> 0 Then
Text1.Text = CommonDialog1.FileName
End If
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDrCogJ1yf7fzECYYzzW4-I6SR3ou1uSgeP06KYMTS4-AeopjHFCoxJWbTziVBVHrWdBdVO7s3mUcdYc4kWP1p87plk7wG5fOvEX1X8SZF0KAIguVToAPJzk2yJOiBW3c-SvhC/s400/CDAfterSelect.gif)
You can restrict the type of files to be selected using the filter Command. Some common filters are
Selecting Microsoft Word Documents
CommonDialog1.Filter = "Microsoft Word Documents (*.doc)*.docMicrosoft Word Documents (*.rtf)*.rtf"
Selecting Image Files
CommonDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)*.BMP;*.JPG;*.GIF"
Selecting Microsoft Word Documents (Including RTF Files)
CommonDialog1.Filter = "Microsoft Word Documents (*.doc;*.rtf)*.doc;*.rtf"
Selecting Microsoft Excel Workbooks
CommonDialog1.Filter = "Microsoft Excel Workbooks (*.xls)*.xls"
Selecting Excel Addins
CommonDialog1.Filter = "Microsoft Excel Addins (*.xla;*.xll)*.xla;*.xll"
Selecting Any files
CommonDialog1.Filter = "All files (*.*)*.*"
Selecting Text files
CommonDialog1.Filter = "Text files (*.txt)*.txt"
Selecting ASCII files
CommonDialog1.Filter = "ASCII files (*.txt;*.log)*.txt;*.log"
See also:
OpenFileDialog in Visual Basic .Net
Search and Open Files using Excel VBA (FileSearch)
Open Excel Files - Open Dialog - GetOpenFilename Method
Selecting a Folder in VB.Net
Browse a Folder / Select a Folder Thru Shell
SaveAs Dialog - Controlled Save