vb读取文本,急

来源:百度知道 编辑:UC知道 时间:2024/05/13 20:10:31
本人刚学编程,很多不会。想知道一个程序。怎么从D盘的1.txt
中读取里面的字符,全部放到textbox控件里面,把第一行放到TEXT1,第二行放到TEXT2....

Private Sub Command1_Click()
Dim i As Integer
Dim a(3) As Variant
i = 0
Open "d:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, b
a(i) = b
i = i + 1
Loop
Text1.Text = a(0)
Text2.Text = a(1)
Text3.Text = a(2)
Close #1
End Sub
上面的数组a的大小你根据自己的数据多少自己改
不过我觉得使用控件数组好点,一个一个的文本框要是数量多了代码很麻烦

'在窗口内建立一个 CommandBox控件,名字为Command1,输入下面代码即可。
Private Sub Command1_Click()
Dim i As Integer, FileName As String, S As String
FileName = "d:\1.txt"
i = FreeFile
Open FileName For Input As #i
Line Input #i, S
Text1.Text = S
Line Input #i, S
Text2.Text = S
Close #i
End Sub

Private Sub Form_Load()
Dim ddd As String '用来存放读入的字符
Open "d:\1.txt" For Input Shared As #1
Do While Not EOF(1)
For i = 0 To 6
Line Input #1, ddd
Text1.Item(i).Text = ddd
Next
Lo