| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2116 人关注过本帖
标题:(新手提问)vb6读取TXT文件内容问题
只看楼主 加入收藏
LeeHl
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-9-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
(新手提问)vb6读取TXT文件内容问题

高手们,我想双击file1里的某个txt文件,然后这个文件内容就自动在text1中显示


我编写了如下代码:
(先放filelistbox1个,textbox 2个)


Private Sub Form_Load()
File2.Path = App.Path & "\VB6"
end sub

Private Sub File1_DblClick()
Dim f As String  '定义f
Dim inp As String ’定义inp
If Right(App.Path, 1) <> "\" Then '作符号判断
f = App.Path & "\" & File1.FileName
Else
f = App.Path & File1.FileName
End If
Text2.Text = f '在一个text2中显示文件地址
Open f For Input As #1 ’打开文件
Line Input #1, inp
Text1.Text = inp '在text1中显示文件内容
Close #1 ’关闭文件
End Sub

双击后就说错误53,文件未找到

这是为什么?

如果将app改为File1,该文件名就会显示在text1中,并不是内容


Private Sub Form_Load()
File1.Path = App.Path & "\VB6"
end sub

Private Sub File1_DblClick()
Dim f As String  '定义f
Dim inp As String ’定义inp
If Right(File1.Path, 1) <> "\" Then '作符号判断
f = File1.Path & "\" & File1.FileName
Else
f = File1.Path & File1.FileName
End If
Text2.Text = f '在一个text2中显示文件地址
Open f For Input As #1 ’打开文件
Line Input #1, inp
Text1.Text = inp '在text1中显示文件内容
Close #1 ’关闭文件
End Sub

这是为什么呢,?高手帮帮忙把错误之处指点下,
重申下
我想双击file1里的某个txt文件,然后这个文件内容就自动在text1中显示

谢谢了!!!
搜索更多相关主题的帖子: 文件 TXT 提问 
2009-09-26 11:34
subMain
Rank: 5Rank: 5
等 级:职业侠客
帖 子:50
专家分:357
注 册:2009-8-17
收藏
得分:20 
是因为File1中的路径和变量f中的路径不一致导致错误。
你可以定义一个存放路径的变量mstrPath。

Private mstrPath As String
 
Private Sub Form_Load()
    mstrPath = App.Path & "\VB6"
     
    File1.Path = mstrPath
End Sub
 
Private Sub File1_DblClick()
    Dim f As String  '定义f
    Dim inp As String '定义inp
     
    If Right(mstrPath, 1) <> "\" Then '作符号判断
        f = mstrPath & "\" & File1.FileName
    Else
        f = mstrPath & File1.FileName
    End If
     
    Text2.Text = f '在一个text2中显示文件地址
     
    Open f For Input As #1 '打开文件
         
         
        Line Input #1, inp
        Text1.Text = inp '在
         
    Close #1 '关闭文件
End Sub

学习VB好多天
2009-09-26 17:34
LeeHl
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-9-20
收藏
得分:0 
我知道我错哪里了

Private Sub File1_DblClick()
Dim f As String
Dim inp As String
    If Right(File1.Path, 1) <> "\" Then
        f = File1.Path & "\" & File1.FileName
    Else
        f = File1.Path & File1.FileName
    End If
Text2.Text = f
Text1.Text = ""
Open f For Input As #1
Do While Not EOF(1)
Line Input #1, inp
Text1.Text = Text1.Text & vbCrLf & inp
Loop
Close #1
Exit Sub
ErrMsg:
MsgBox "未找到文件!" & vbNewLine & f
End Sub

嘻嘻。改过自新~~~
2009-09-26 21:33
快速回复:(新手提问)vb6读取TXT文件内容问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019268 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved