文本内容替换及通用对话框使用

来源:百度知道 编辑:UC知道 时间:2024/06/30 23:02:27
我画了一个CommandButton,一个Label
添加代码如下:
Private Sub Command1_Click()
CommonDialog1.DialogTitle = "选择路径"
CommonDialog1.ShowOpen
Label1.Caption = CommonDialog1.FileName
End Sub

打开通用对话框以选择路径,并在Label上显示得到的文件路径。
下面问题来了:

我要把得到的路径字符用来替换默认路径下的1.txt文件中的“你好吗?”这段文字,代码应该怎么写(请在以上的代码基础上进行修改)

另:
如果要让打开通用对话框时直接打开某个文件夹,就是做一个默认打开的文件夹,比如“D:\Games”,代码应该怎样写?

还有:
怎样让打开的通用对话框只显示某一类文件?

最后一个问题:
上面的代码是选择某个文件,如果要让他选择某个文件夹呢

最后多谢大家了。
请在原代码的基础上进行修改。
完整代码的话,追加30分。谢谢!谢谢!

第1问.你说的那个“默认路径”应该指App.Path 所指路径吧。代码如下。
===============
Private Sub Command1_Click()
CommonDialog1.DialogTitle = "选择路径"
CommonDialog1.ShowOpen
Label1.Caption = CommonDialog1.FileName
Open App.Path & "\1.txt" For Input As 1
s = StrConv(InputB(LOF(1), 1), vbUnicode)
s = Replace(s, "你好吗?", Label1.Caption)
Close
Open App.Path & "\1.txt" For Output As 1
Print #1, s
Close
End Sub

==============
第2问.Private Sub Command1_Click()
CommonDialog1.DialogTitle = "选择路径"
CommonDialog1.InitDir = "d:\games"
CommonDialog1.ShowOpen
Label1.Caption = CommonDialog1.FileName
End Sub
==============
第3问.Private Sub Command1_Click()
CommonDialog1.DialogTitle = "选择路径"
CommonDialog1.Filter = "文本文件|*.txt"
CommonDialog1.ShowOpen
Label1.Caption = CommonDialog1.FileName
End Sub