| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1414 人关注过本帖
标题:如何将TXT文本的内容导入到窗体上的TEXT中
只看楼主 加入收藏
liuwei1012
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-4-3
收藏
 问题点数:0 回复次数:2 
如何将TXT文本的内容导入到窗体上的TEXT中

请问:
用什么方法能将TXT文件导入.到窗体上的TEXT控件里显示呢??
能附源程序吗??拜托!!!!

搜索更多相关主题的帖子: TXT TEXT 窗体 文本 
2006-04-29 11:21
leon2
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:731
专家分:0
注 册:2005-3-18
收藏
得分:0 
Open 文件名 For Input As #1
Do While Not EOF(1)
Line Input #1, Text1.Text
Loop
2006-04-29 22:02
ChenMo
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:481
专家分:10
注 册:2004-4-8
收藏
得分:0 

'说明:
' 以下程序先将所列代码保存到一份模块文件内

' 导入模块到工程后,需要引用文件系统对象(关于文件系统的详细使用方法请查阅 MSDN)
' 引用文件系统对象的方法十分简单:
' 选择[工程]菜单 -> 引用 -> Microsoft Scripting Runtime(勾选然后确定即可)
'
' 本模块只有一个函数 ImportFile
' 函数参数说明:
' ctlText - 此参数准备接受一个对象,此对象只能是 TextBox 或 RichTextBox,除此以外的对象均会引起错误:未知错误
' sFileName - 被导入的文件全路径,若文件路径无效,函数返回 False, 引发错误: 文件未找到
' bDisplayFileInfo - 显示文件信息
' 成功调用 ImportFile 返回 True,否则返回 False,并将 impfErrMsg 设置错误信息
'
' 调用示例:
' (1) TextBox
' ImportFile Text1, "C:\Temp.txt", False
' 或(Test 为 Boolean 类型变量,用于存储返回值)
' Test = ImportFile(Text1, "C:\Temp.txt", False)
' (2) RichTextBox
' ImportFile RichTextBox1, "C:\Temp.txt", False
' 或(Test 为 Boolean 类型变量,用于存储返回值)
' Test = ImportFile(RichTextBox1, "C:\Temp.txt", False)
'
' 注意:
' 在设计时需先将显示多行属性 MultiLine 设置为 True,否则文件若有多行将无法显示多行文本。

Option Explicit

Public impfErrMsg As String '当有错误时,此变量带回最后一次错误的信息

Public Function ImportFile(ByRef ctlText As Control, ByVal sFileName As String, ByVal bDisplayFileInfo As Boolean) As Boolean
On Error GoTo ErrHandle

Dim fObject As FileSystemObject
Dim fTextStream As TextStream
Dim fFile As File

Set fObject = CreateObject("Scripting.FileSystemObject") '创建文件系统对象
If fObject.FileExists(sFileName) = False Then '验证文件是否存在
impfErrMsg = "文件未找到。"
ImportFile = False
Exit Function
End If


Set fFile = fObject.GetFile(sFileName)
Set fTextStream = fObject.OpenTextFile(sFileName, ForReading, True)

ctlText.Text = ""

'显示文件信息
If bDisplayFileInfo = True Then
ctlText.Text = ctlText.Text & "文件名: " & fFile.Name & vbCrLf
ctlText.Text = ctlText.Text & "路径: " & fFile.Path & vbCrLf
ctlText.Text = ctlText.Text & "创建日期: " & fFile.DateCreated & vbCrLf
ctlText.Text = ctlText.Text & "最后访问日期: " & fFile.DateLastAccessed & vbCrLf
ctlText.Text = ctlText.Text & "最后修改日期: " & fFile.DateLastModified & vbCrLf
ctlText.Text = ctlText.Text & "文件大小: " & fFile.Size & vbCrLf
ctlText.Text = ctlText.Text & "文件类型: " & fFile.Type & vbCrLf
ctlText.Text = ctlText.Text & "------------------------------" & vbCrLf
End If

'开始读入到 TextBox/RichTextBox

ctlText.Text = ctlText.Text & fTextStream.ReadAll
ImportFile = True

fTextStream.Close
Set fFile = Nothing
Set fObject = Nothing

Exit Function
ErrHandle:
impfErrMsg = "在导入文件时发生错误。"
ImportFile = False
fTextStream.Close
Set fFile = Nothing
Set fObject = Nothing
End Function


[此贴子已经被作者于2006-4-30 14:18:29编辑过]


欢迎加入 MVC 技术讨论群(新群:90093426)
2006-04-30 14:17
快速回复:如何将TXT文本的内容导入到窗体上的TEXT中
数据加载中...
 
   



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

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