易截截图软件、单文件、免安装、纯绿色、仅160KB

VB 读指定行

http://zhidao.baidu.com/question/81532606.html
VB 读指定行
悬赏分:10 - 解决时间:2009-1-20 11:41
vb中如何读取指定行的内容,窗体上有两个标签,文件在C:\A.TXT,四个按钮,单击第一个,读文件的第一行到标签一,第二行到标签二;单击第二个按钮,第三行到标签一,第四行到标签二;以此类推,谢谢
提问者: imyangmo - 书生 二级
最佳答案
用文件系统可以读取指定行。。。但是如果文件内容不是很多的话用顺序文件全部读出来截断到数组后提取指定行更方便。。。
使用文件系统读取指定行:
首先需要“引用”Microsoft Scripting Runtime
Private Sub Command1_Click()
Dim FSO As New FileSystemObject
Dim TS As TextStream
Dim Str As String
If Dir("C:\A.txt") <> "" Then '判断文件是否存在
Set TS = FSO.OpenTextFile("C:\A.txt", ForReading) 'ForReading为读模式,另外有写和追加模式
Do Until TS.Line = 1 '跳到指定行时终止循环
TS.SkipLine '跳过一行
Loop
Str = TS.ReadLine '读出行,文件系统读写位置就是光标所在的位置
Print Str
Else
MsgBox "文件不存在!"
End If
'但是文件系统好像不能写指定行
End Sub
----------------------------------------------------------------
使用顺序文件获取指定行内容
Private Sub Command1_Click()
Dim Arr
Dim Str As String
Dim i As Integer
Open "C:\A.txt" For Binary As #1
Str = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
Arr = Split(Str, vbCrLf)
For i = 0 To UBound(Arr)
Print Arr(i)
Next i
End Sub


相关文档:

VB 中颜色互相转换的两个小函数(REG和 VB颜)

VB颜色和RGB色互相转换头疼了我好一段时间,如今找到解决办法了
特别贴出来与大家共享 
Type RGB
    Red As String
    Green As String
    Blue As String
End Type
Public Function ColorToRGB(ByVal dwColor As Long) As RGB
    Dim clrHex ......

一些VB相关的东西~~


珍贵vb函数列表
Variant 变量的内部表示:
_______________________________________________________________
符号常量 值 内部表示 字节数
V_EMPTY 0 Empty
V_NULL 1 Null
V_INTEGER 2 Interger 2
V_LONG 3 Long 4
V_SINGLE 4 Single 4
V_DOUBLE 5 Double 8
V_CURRENCY 6 Currency 8
V_DATE 7 Date 8
V_STRI ......

VB调用系统的"打印机设置"和"页面设置".

VB调用系统的"打印机设置"和"页面设置".
Option Explicit
Const FW_NORMAL = 400
Const DEFAULT_CHARSET = 1
Const OUT_DEFAULT_PRECIS = 0
Const CLIP_DEFAULT_PRECIS = 0
Const DEFAULT_QUALITY = 0
Const DEFAULT_PITCH = 0
Const FF_ROMAN = 16
Const CF_PRINTERFONTS = &H2
Const C ......

VB使用手形的图标

Option Explicit
Private Declare Function LoadCursor Lib "user32.dll" Alias "LoadCursorA" (ByVal hInstance As Long, ByVal lpCursorName As Long) As Long
Private Declare Function SetCursor Lib "user32.dll" (ByVal hCursor As Long) As Long
Private Const IDC_HAND As Long = 32649
Private myHand_handle ......

vb使用picturebox画曲线图

'添加个picture1和command1
Option Explicit
Dim lngCenter As Long
Dim lngMax As Long
Dim lngPad&
Dim PCurrent As POINT, PLast As POINT
Private Type POINT
x As Long
y As Long
End Type
Private Sub Form_Load()
lngCenter = (Picture1.Top + Picture1.Height) / 2
lngMax = Pictu ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号