求助:VB中怎样由菜单选中文本文件,并由文本框打开?急!

来源:百度知道 编辑:UC知道 时间:2024/07/08 21:12:49
就是单击菜单→打开(mnuOpen),希望能出现类似于Word打开文件的那种窗口,但只显示文本文件。
选中文件后,文件能在文本框(text1)中打开。

急,拜托各位了……

Private Sub mnuOpen_Click()
Dim oDLG
Dim b() As Byte, S As String
Set oDLG = CreateObject("MSComDlg.CommonDialog")
With oDLG
.DialogTitle = "打开文件"
.Filter = "文本文件|*.txt"
.MaxFileSize = 255
.ShowOpen
If .FileName <> "" Then
ReDim b(FileLen(.FileName))
Open .FileName For Binary As #1
Get #1, , b
Close #1
Text1.Text = StrConv(b, vbUnicode)

End If
End With
Set oDLG = Nothing

End Sub

很简单的,用commanddialog控件就行了,上网找一下用法,几分钟就会了

CommonDialog1.Filter = "文档文件(*.txt)|*.txt|"
CommonDialog1.ShowOpen
dim a as string , b as string
Open CommonDialog1.FileName For Input As #1
Do Until EOF(1)
Input #1, a
b = b & a
Loop
text1.text =