vb怎么实现d出对话框选择文件路径

vb怎么实现d出对话框选择文件路径,第1张

VB6.0使用CommonDialog 控件d出对话框选择文件路径。

通过使用 CommonDialog 控件的 ShowOpen 和 ShowSave

方法可显示“打开”和“另存为”对话框。

两个对话框均可用以指定驱动器,目录,文件扩展名和文件名。除对话的标题不同外,另存为对话外观上与打开对话相似。

下例显示“打开”对话框然后在信息框中显示所选的文件名:

Private Sub Command1_Click()

    ' 设置“CancelError”为 True

    CommonDialog1.CancelError = True

    On Error GoTo ErrHandler

    ' 设置标志

    CommonDialog1.Flags = cdlOFNHideReadOnly

    ' 设置过滤器

    CommonDialog1.Filter = "All Files (*.*)|*.*|Text Files" & _

    "(*.txt)|*.txt|Batch Files (*.bat)|*.bat"

    ' 指定缺省的过滤器

    CommonDialog1.FilterIndex = 2

    ' 显示“打开”对话框

    CommonDialog1.ShowOpen

    ' 显示选定文件的名字

    MsgBox CommonDialog1.FileName '显示路径

    Exit Sub

ErrHandler:

    ' 用户按了“取消”按钮

    Exit Sub

End Sub

打开文件对话框,添加CommonDialog控件就可以。

添加方法:【工程】-【部件】在部件对话框勾选:Microsoft Common Dialog Control 6.0 (SP3)

这样控件添加在工具箱中了,拖动到界面即可。

主要代码:

CommonDialog1.InitDir = "c:/" '对话框初始目录

CommonDialog1.Filter = "文本文件 (.txt)|*.txt|图片(.gif)|*.gif"'设置打开文件类型

CommonDialog1.ShowOpen'打开文件对话框

'CommonDialog1.ShowSave '保存文件对话框

'CommonDialog1.ShowColor'打开颜色设置对话框 等等


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12007258.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存