VB 作业 遇阻 求各位大大抬手指点

来源:百度知道 编辑:UC知道 时间:2024/06/11 12:10:13
编写一个程序,在窗体上放置一个内容为你自己的姓名的按钮,当用户点击该按钮时显示打开文件对话框,该对话框默认显示所有后缀是frm和vbp的文件(也可选择所有文件),其默认路径为D盘上你的学号姓名的文件夹。当用户选择文件后,将窗体的标题设置为该文件名,并将按钮上的文字修改为你的学号。
求教 解题过程

Private Sub Command1_Click()
CommonDialog1.InitDir = "D:" '设置默认路径

CommonDialog1.CancelError = True ' 设置“CancelError”为 True
On Error GoTo ErrHandler

CommonDialog1.Flags = cdlOFNHideReadOnly ' 设置标志

CommonDialog1.Filter = "All Files (*.*)|*.*|(*.frm;*.vbp)|*.frm;*.vbp" ' 设置过滤器

CommonDialog1.FilterIndex = 2 ' 指定缺省的过滤器

CommonDialog1.ShowOpen ' 显示“打开”对话框

MsgBox CommonDialog1.FileName ' 显示选定文件的名字
Command1.Caption = Getfilename(CommonDialog1.FileName) '更改按钮名
Me.Caption = Left(Getfilename(CommonDialog1.FileName), 3) '更改标题名 细节自行修改
Exit Sub
ErrHandler:
Exit Sub ' 用户按了“取消”按钮
End Sub

Private Sub Form_Load()
Command1.Caption = "姓名"
End Sub

Function Getfilename(FPath)
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(FPath) = True Then
Set pname =