form1代码(包裹两个command,一个list,一个commondialog)
commonand1为选择文件按钮,另一个是退出
Option ExplicitPrivate Sub Command1_Click()
Dim DlgInfo As DlgFileInfo
Dim I As Integer
On Error GoTo ErrHandle
'清除List1中的项
List1.Clear
'选择文件
With CommonDialog1
.CancelError = True
.MaxFileSize = 32767 '被打开的文件名尺寸设置为最大,即32K
.Flags = cdlOFNHideReadOnly Or cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNNoDereferenceLinks
.DialogTitle = "选择文件"
.Filter = "所有类型的文件(*.*)|*.*"
.ShowOpen
DlgInfo = GetDlgSelectFileInfo(.FileName)
.FileName = "" '在打开了*.pif文件后须将Filename属性置空,
'否则当选取多个*.pif文件后,当前路径会改变
End With
For I = 1 To DlgInfo.iCount
List1.AddItem DlgInfo.sPath & DlgInfo.sFile(I)
Next I
Exit SubErrHandle:
' 按了“取消”按钮End SubPrivate Sub Command2_Click()
End
End Sub
Private Sub Form_Load()End Sub
标准模块代码
Option ExplicitType DlgFileInfo
iCount As Long
sPath As String
sFile() As String
End Type
Public Function GetDlgSelectFileInfo(strFilename As String) As DlgFileInfo
Dim sPath, tmpStr As String
Dim sFile() As String
Dim iCount As Integer
Dim I As Integer
On Error GoTo ErrHandle
sPath = CurDir() '获得当前的路径,因为在CommonDialog中改变路径时会改变当前的Path
tmpStr = Right$(strFilename, Len(strFilename) - Len(sPath)) '将文件名分离出来
If Left$(tmpStr, 1) = Chr$(0) Then
'选择了多个文件(表现为第一个字符为空格)
For I = 1 To Len(tmpStr)
If Mid$(tmpStr, I, 1) = Chr$(0) Then
iCount = iCount + 1
ReDim Preserve sFile(iCount)
Else
sFile(iCount) = sFile(iCount) & Mid$(tmpStr, I, 1)
End If
Next I
Else
'只选择了一个文件(注意:根目录下的文件名除去路径后没有"\")
iCount = 1
ReDim Preserve sFile(iCount)
If Left$(tmpStr, 1) = "\" Then tmpStr = Right$(tmpStr, Len(tmpStr) - 1)
sFile(iCount) = tmpStr
End If
GetDlgSelectFileInfo.iCount = iCount
ReDim GetDlgSelectFileInfo.sFile(iCount)
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
GetDlgSelectFileInfo.sPath = sPath
For I = 1 To iCount
GetDlgSelectFileInfo.sFile(I) = sFile(I)
Next I
Exit FunctionErrHandle:
MsgBox "GetDlgSelectFileInfo函数执行错误!", vbOKOnly + vbCritical, "自定义函数错误"End Function
commonand1为选择文件按钮,另一个是退出
Option ExplicitPrivate Sub Command1_Click()
Dim DlgInfo As DlgFileInfo
Dim I As Integer
On Error GoTo ErrHandle
'清除List1中的项
List1.Clear
'选择文件
With CommonDialog1
.CancelError = True
.MaxFileSize = 32767 '被打开的文件名尺寸设置为最大,即32K
.Flags = cdlOFNHideReadOnly Or cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNNoDereferenceLinks
.DialogTitle = "选择文件"
.Filter = "所有类型的文件(*.*)|*.*"
.ShowOpen
DlgInfo = GetDlgSelectFileInfo(.FileName)
.FileName = "" '在打开了*.pif文件后须将Filename属性置空,
'否则当选取多个*.pif文件后,当前路径会改变
End With
For I = 1 To DlgInfo.iCount
List1.AddItem DlgInfo.sPath & DlgInfo.sFile(I)
Next I
Exit SubErrHandle:
' 按了“取消”按钮End SubPrivate Sub Command2_Click()
End
End Sub
Private Sub Form_Load()End Sub
标准模块代码
Option ExplicitType DlgFileInfo
iCount As Long
sPath As String
sFile() As String
End Type
Public Function GetDlgSelectFileInfo(strFilename As String) As DlgFileInfo
Dim sPath, tmpStr As String
Dim sFile() As String
Dim iCount As Integer
Dim I As Integer
On Error GoTo ErrHandle
sPath = CurDir() '获得当前的路径,因为在CommonDialog中改变路径时会改变当前的Path
tmpStr = Right$(strFilename, Len(strFilename) - Len(sPath)) '将文件名分离出来
If Left$(tmpStr, 1) = Chr$(0) Then
'选择了多个文件(表现为第一个字符为空格)
For I = 1 To Len(tmpStr)
If Mid$(tmpStr, I, 1) = Chr$(0) Then
iCount = iCount + 1
ReDim Preserve sFile(iCount)
Else
sFile(iCount) = sFile(iCount) & Mid$(tmpStr, I, 1)
End If
Next I
Else
'只选择了一个文件(注意:根目录下的文件名除去路径后没有"\")
iCount = 1
ReDim Preserve sFile(iCount)
If Left$(tmpStr, 1) = "\" Then tmpStr = Right$(tmpStr, Len(tmpStr) - 1)
sFile(iCount) = tmpStr
End If
GetDlgSelectFileInfo.iCount = iCount
ReDim GetDlgSelectFileInfo.sFile(iCount)
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
GetDlgSelectFileInfo.sPath = sPath
For I = 1 To iCount
GetDlgSelectFileInfo.sFile(I) = sFile(I)
Next I
Exit FunctionErrHandle:
MsgBox "GetDlgSelectFileInfo函数执行错误!", vbOKOnly + vbCritical, "自定义函数错误"End Function
解决方案 »
- vb6 listview 分页程序
- 系统错误&H8007007E(-214724770),找不到指点的模块
- 求在图片框中显示图片的代码,在线等!!!
- 在VB中如何声明、调用外部DLL中需要传递函数指针的API函数原型,急!!!
- 如何在未安装vb的机器上运行带ActiveX控件的程序呀?急死我了!
- 在VB中把数据转换成Excel有时出错,为什么?3天了,没人回答,真难吗?
- 小問題
- 高手的眼球请到这里来:80分超值大奉送
- 菜鸟提问:如何读写隐藏文件包括copy del?
- 请教如何在vb中将命令按钮设为左边为图标,右边为文字
- VC中有这样一条语句msgsock = connect(ServerSock,(struct sockaddr*)&server,sizeof(server));用VB我是这样写的?
- 盼高手作答:怎么在应用程序中,拥有类似VB的外部插件扩展的能力
Private Sub Command1_Click()
Dim I As Integer
Dim Y As Integer
Dim Z As Integer
Dim FileNames$()
Const OFN_ALLOWMULTISELECT = &H200&
CommonDialog1.filename = ""
CommonDialog1.Filter = "All Files|*.*"
CommonDialog1.Flags = OFN_ALLOWMUL- SELECT
CommonDialog1.Action = 1
CommonDialog1.filename = CommonDialog1. filename & Chr(3 2)
Z = 1
For I = 1 To Len(CommonDialog1.filename)
I = InStr(Z, CommonDialog1.filename, Chr(32))
If I = 0 Then Exit For
ReDim Preserve FileNames(Y)
FileNames(Y) = Mid(CommonDialog1.filename, Z, I - Z)
Z = I + 1
Y = Y + 1
Next
If Y = 1 Then
Text1.Text = FileNames(0)
Else
Text 2.Text = ""
For I = 0 To Y - 1
If I = 0 Then
Text1.Text = FileNames(I)
Else
Text2.Text = Text2.Text & UCase(FileNames(I)) & Chr$(13) & Chr$(10)
End If
Next
End If
End Sub
CommonDialog1.Flags = cdlOFNAllowMultiselect