vb 怎么把commondialog选中的文件加到list的列表里 谢谢啊

2025-03-03 19:14:12
推荐回答(2个)
回答1:

Dim i As Long, x As Long
With CommonDialog1 '对CommonDialog的设置
.InitDir = List2.List(L)
.DialogTitle = "添加文件"
'ToDo: 设置 common dialog 控件的标志和属性
.Filter = "所有文件 (*.*)|*.*"
.Flags = cdlOFNAllowMultiselect Or cdlOFNExplorer Or cdlOFNNoChangeDir
.InitDir = Replace(List2.List(L), "\\", "\")
.CancelError = False
.ShowOpen

If Len(.filename) = 0 Then
Exit Sub
End If

FileManyAddress = .filename

End With

Files = Split(FileManyAddress, Chr(0))

'-----单一文件处理部分----
If UBound(Files) = 0 Then
ReDim Preserve Files(1)

i = InStrRev(Files(0), "\")
Files(1) = Mid(Files(0), i + 1)
Files(0) = Left(Files(0), i - 1)
End If

For i = 1 To UBound(Files) 'files(0) 就是这些文件的路径
For x = 0 To List3.ListCount
If Files(i) = List3.List(x) Then Exit For
Next
If Files(i) = List3.List(x) Then MsgBox Files(i) + Space(2) + "重复添加!", vbInformation + vbOKOnly, "重复的文件名"
If Files(i) <> List3.List(x) Then List3.AddItem Files(i)
Next
这是点击一个按钮,弹出一个对话框,当你选择好文件确定后就在list3中添加对应的文件名!

回答2:

建一个CommonDialog1,Command1,List1。
代码如下。
====================
Private Sub Command1_Click()
CommonDialog1.ShowOpen
List1.AddItem CommonDialog1.FileName
End Sub