Private Sub Command1_Click()
CommonDialog1.Filter = "文本文档(*.txt)"
CommonDialog1.ShowOpenDim Lstr As String
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
Line Input #1, Lstr
Text1.Text = Text1.Text & Lstr & vbCrLf
Loop
Close #1
End Sub
这样读取倒是好使,但是文件太大了,就不好使了,窗口会没有相应这怎么办啊?
大虾帮帮忙啊~!!还有这个图片里面的是什么控件啊?谁帮我看一下
而且编程处理字符串相连很费时的,最好一次性读入(比如使用fso,readfile[api]等),这样速度很快,也不用考虑窗体不能响应的问题。
Do While Not EOF(1)
Line Input #1, Lstr
Lstr= Lstr & vbCrLf
Loop
Text1.Text = Lstr2.那个象是mshflexgrid或msflexgrid表格控件
tmp = tmp + Lstr + vbCrLf
Text1.Text = tmp
第二行蓝色这样可以吗?
Option ExplicitPrivate Sub Command1_Click()
Dim hFile As Integer
Dim aBytes() As Byte
CommonDialog1.Filter = "文本文档(*.txt)"
CommonDialog1.ShowOpen hFile = FreeFile()
Open CommonDialog1.FileName For Binary Access Read As #hFile
ReDim aBytes(LOF(hFile) - 1)
Get #hFile, , aBytes
Close #hFile
Text1.Text = StrConv(aBytes, vbUnicode)
End Sub
下面那个控件好像是 listview
谢谢啊我的文件看了大雨64K,我用RichTextBox 可以读取文件但是我想设置成一行一个颜色,可以做的到吗??我这个小程序需要设置颜色才可以
全给1楼
Dim hFile As Integer
Dim aBytes() As Byte
Dim aLines() As String
Dim i As Long
CommonDialog1.Filter = "文本文档(*.txt)"
CommonDialog1.ShowOpen hFile = FreeFile()
Open CommonDialog1.FileName For Binary Access Read As #hFile
ReDim aBytes(LOF(hFile) - 1)
Get #hFile, , aBytes
Close #hFile
aLines = Split(StrConv(aBytes, vbUnicode), vbCrLf)
RichTextBox1.Text = vbNullString
For i = 0 To UBound(aLines)
RichTextBox1.SelColor = IIf(i Mod 2, vbBlue, vbRed)
RichTextBox1.SelText = aLines(i) & vbCrLf
Next
RichTextBox 比普通 TextBox 慢多了。
真正的大文件如果不需要编辑的话,还是用图片框和滚动条模拟,只需要输出可见的一部分。
Private Sub Command1_Click()
RichTextBox1.Text = ""
CommonDialog1.Filter = "文本文档(*.txt)"
CommonDialog1.ShowOpenDim Lstr As String
Open CommonDialog1.FileName For Input As #1
Dim start As Integer
Dim length As Integer
Dim i As Integer
i = 0
Do While Not EOF(1)
Input #1, Lstr
RichTextBox1.SetFocus
RichTextBox1.SelStart = Len(RichTextBox1.Text)
'去掉空格
If Len(Lstr) = 0 Then
'MsgBox ("null")
Else
'添加到文本框中
start = Len(RichTextBox1)
RichTextBox1.Text = RichTextBox1.Text & Lstr & vbCr
length = Len(RichTextBox1)
End If
With RichTextBox1
.SelStart = start
.SelLength = length
Select Case i
Case 0
.SelColor = vbRed
Case 1
.SelColor = vbGreen
End Select
.SelBold = True
.SelUnderline = True
End With
If i = 0 Then
i = 1
Else
i = 0
End If
Loop
Close #1End Sub
这个是我的一个按钮的事件,我选择文件之后一行一行读取,在把文字一行用红色一行用绿色怎么不行啊?一点效果都没有
我刚看vb就一天我看了你写的,比我的加载速度快多了,而且代码更短。我是想问一下你的是 Open CommonDialog1.FileName For Binary Access Read As #hFile
'LOF 对于尚未打开的文件,使用 FileLen 函数将得到其长度。
ReDim aBytes(LOF(hFile) - 1)
'读入到byte中
Get #hFile, , aBytes
Close #hFile'在通过split函数把byte以回车分开存入数组当中
aLines = Split(StrConv(aBytes, vbUnicode), vbCrLf)
'循环遍历
RichTextBox1.Text = vbNullString
For i = 0 To UBound(aLines)
'i 于 2 ==0 就 用蓝色,不等于就用红色
'RichTextBox1.SelColor = IIf(i Mod 2, vbBlue, vbRed)
MsgBox (aLines(i))
'选中文字,并赋值吗?
RichTextBox1.SelText = aLines(i) & vbCrLf
Next
你的代码我这么理解的对不?
2)RTF : Rich Text Format
RichTextBox1.SelText = aLines(i) & vbCrLf你的2行修改怎么OK呢?是因为用了SelText吗?