当前位置:编程学习 > VB >>

VB编程问题关于input

我想单机command按钮就读取C盘1.txt的文件内容到text中,怎么写代码
追问:来自手机问问谢谢帮忙能给个注释那就好了,入问问团队已经不必麻烦了
答案:Private Sub Command1_Click()
Open "C:\1.txt" For Input As #1 ‘c:\1.txt这里是写文件地址的 for input 是输出的意思 #1标记
Dim TTBBA As String ’声明TTBBA为字符串型
Do While Not EOF(1) ‘NOT EOF (NOt)就是不等。那么不等于EOF就是不是结尾数据没有到最后的意思
Line Input #1,TTBBA 'line一行一行输出input到TTBBA
Text1.Text = Text1.Text & TTBBA & vbCrLf ’将变量的值传递给text文本框保存,并换行(vbCrlf是换行符)
Loop '循环结束
Close #1 ‘关闭文件标记#1
End Sub
欢迎来VB之家学习。可以百度下VB之家

 

楼上几位不懂别误导别人,下面的代码才是正确的

Private Sub Command1_Click()
Open "C:\1.txt" For Input As #1
Dim file As String
Do While Not EOF(1)
Line Input #1, file
Text1.Text = Text1.Text & file & vbCrLf
Loop
Close #1
End Sub

ps:楼主有问题欢迎追问,有错误欢迎指出,若有空欢迎加入本团~

 

楼上的几位的回答我都看过了,第一位纯属复制粘贴,第二位下标越界,且代码很混乱,完全没必要定义数组,占用内存。


调用API的功能实现,
因相应的API较复杂,不太好理解,我用子程序对它做了封装。
实际使用过程中你将下面这段代码Copy到程序模块中,然后调用SubOpenFile子程序就行了。
'注意在引用该FSO功能时先引用 Microsoft Scripting Runtime
Public ObjFso As New FileSystemObject
Declare Function shellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Sub SubOpenFile(ByVal FileSaveName As String, ByVal StrFrmName As Form)
'打开文件
'输入:sFileName 待打开文件的全路径加上文件名,如 "F:\VB\RunExe\Module1.bas"
'输入:调用该子程序的窗体名称
If Not ObjFso.FileExists(FileSaveName) Then
MsgBox ("没有找到要打开的文件")
Exit Sub
End If
sCorrectPath = ObjFso.GetParentFolderName(FileSaveName)
sFileName = ObjFso.GetFileName(FileSaveName)
'输入:调用该子程序的窗体名称
lHwnd = StrFrmName.hwnd
'lShellFile = shellExecute(lHwnd, "open", FileSaveName, vbNullString, sCorrectPath, SW_SHOWNORMAL)
lShellFile = shellExecute(lHwnd, "open", FileSaveName, vbNullString, sCorrectPath, 5)
'错误处理
If lShellFile > 32 Then
Exit Sub
Else
Select Case lShellFile
Case 2
If Right(sFileName, 3) <> "htm" Then
MsgBox "File Not Found.", vbCritical vbOKOnly, "X-File:"
End If
Exit Sub
Case 3
MsgBox "Path not Found.", vbCritical vbOKOnly, "X-File:"
Exit Sub
Case 5
MsgBox "Access denied.", vbCritical vbOKOnly, "X-File:"
Exit Sub
Case 8
MsgBox "Out of Memory.", vbCritical vbOKOnly, "X-File:"
Exit Sub
Case 32
MsgBox "Shell32.dll not Found.", vbCritical vbOKOnly, "X-File:"
Exit Sub
End Select
End If









Private Sub Command1_Click()
Dim TempFile As Long
Dim LoadBytes() As Byte

TempFile = FreeFile
Open "c:\2.txt" For Binary As #TempFile
ReDim LoadBytes(1 To LOF(TempFile)) As Byte
Get #TempFile, , LoadBytes
Close TempFile

Text1.Text = StrConv(LoadBytes, vbUnicode)


End Sub

Private Sub Command1_Click()
    dim file as string
    dim sText as string,sLine as string
    'Laserw9手写的。
    file="C:\1.txt"
    open file for input as #1
    do while not eof(1)
        line input #1,sLine
        sText=sText & sLine
    loop
    close #1
    msgbox "读取的内容为" & sText
End Sub

上一个:vb中重命名问题。
下一个:求vb代码一段

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,