注册 登录
编程论坛 VB6论坛

【分享】VB6 選擇資料夾or檔案

kedian1968 发布于 2022-03-10 16:08, 1628 次点击
問題
CommonDialog能選擇資料夾嗎?
Shl.BrowseForFolder 能秀全資料夾 又 預設資料夾嗎? 查到的預設C:\ 後 D:\不會顯示

只有本站会员才能查看附件,请 登录

程序代码:

Private Sub Command1_Click() '選資料夾
    Dim Shl As Object
    Set Shl = CreateObject("Shell.Application") '創物件
    Set Fd = Shl.BrowseForFolder(0, "選取資料夾", 0, "\") '標題 and 預設路徑\
    If Not Fd Is Nothing Then '非錯誤執行
        Text1.Text = Fd.Items.Item.Path ' 顯示所選取檔案的名稱
    End If
    Set Shl = Nothing '釋放
    Set Fd = Nothing '釋放
End Sub

Private Sub Command2_Click() '選檔案 需物件CommonDialog1
  On Error Resume Next ' 略過錯誤
  CommonDialog1.Flags = cdlOFNHideReadOnly '單選 cdlOFNAllowMultiselect '改為復選
  ' 限制檔案類型
  CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files(*.txt)|*.txt|Batch Files (*.bat)|*.bat"
  CommonDialog1.FilterIndex = 2   ' 預設第2 "|" 項目
  CommonDialog1.ShowOpen ' 顯示「開啟舊檔」對話方塊
  Text1.Text = CommonDialog1.FileName ' 顯示所選取檔案的名稱
End Sub


參考來源
選取資料夾
http://
選擇檔案
http://yes.nctu.edu.tw/vb/2_Ctrl/CommonDialog_ShowOpenSave_Demo.htm
CommonDialog.flags 參數含義
https://blog.
2 回复
#2
cwa99582022-03-11 08:00
看不明白。
Command1只是显示文件夹(资料夹),
Text1.Text = Fd.Items.Item.Path ' 顯示所選取檔案的名稱
这句也只是显示文件夹,没有显示具体的文件名。
应该没有问题的,
查到的預設C:\ 後 D:\不會顯示,是怎么不能显示?
#3
kedian19682022-03-11 10:26
Set Fd = Shl.BrowseForFolder(0, "選取資料夾", 0, "C:\")
設成預設C:\ 其他槽看不到

CommonDialog1開啟舊檔 只能選擇檔案
有方式用 CommonDialog1 選資料夾嗎

1