要在VB.NET中使用打开选择文件和打开选择文件夹对话框,可以借助System.Windows.Forms中的OpenFileDialog和FolderBrowserDialog对话框。
其中OpenFileDialog对话框可以提供选择文件,FolderBrowserDialog对话框可以提供选择文件夹。
以下代码是选择文件的通用代码:
Imports lbForm = System.Windows.Forms Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim obj As New lbForm.OpenFileDialog Dim sFile As String With obj .ShowDialog() sFile = .FileName If Len(sFile) Then MsgBox("你选择了" & sFile & "文件") Else MsgBox("你没有选择文件") End If End With End Sub End Class
以下代码是选择文件夹对话框的通用代码:
Imports lbForm = System.Windows.Forms Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim obj As New lbForm.FolderBrowserDialog Dim sPath As String With obj .ShowDialog() sPath = .SelectedPath If Len(sPath) Then MsgBox("你选择了" & sPath & "文件夹") Else MsgBox("你没有选择文件夹") End If End With End Sub End Class
发表评论