读书人

VB读取文本行,该如何解决

发布时间: 2012-02-04 15:43:09 作者: rapoo

VB读取文本行
VB如何读取文本 每行显示在label标签上 当读取到文本末尾是提示文本已结束


一个command click事件就是跳到指定行




[解决办法]
放置一个label,一个command,C盘下面放一个包含内容的1.txt

VB code
Option ExplicitDim strLine As StringPrivate Sub Command1_Click()Do While Not EOF(1)Line Input #1, strLineLabel1.Caption = strLineExit DoLoopIf EOF(1) Then    Label1.Caption = "文章结束!"    Command1.Enabled = FalseEnd IfEnd SubPrivate Sub Form_Load()Label1.AutoSize = TrueCommand1.Enabled = FalseOpen "c:\1.txt" For Input As #1Do While Not EOF(1)Line Input #1, strLineLabel1.Caption = strLineExit DoLoopCommand1.Enabled = TrueEnd Sub
[解决办法]
用控件数组:

VB code
Option ExplicitPrivate Sub Command1_Click()  Dim strLine As String  Dim i As Integer  For i = 0 To 4    If EOF(1) Then      Label1(i).Caption = "文章结束!"      Command1.Enabled = False      Close #1      Exit Sub    End If    Line Input #1, strLine    Label1(i).Caption = strLine  Next iEnd SubPrivate Sub Form_Load()  Dim i As Integer  for i = 0 To 4    Label1(i).AutoSize = True  Next i  Open "c:\1.txt" For Input As #1  Command1_ClickEnd Sub 

读书人网 >VB

热点推荐