1

我有一个带有以下行的文本文件:

11/01/2013  00:57:59    01  00  0238    POST UNIT ID

11/01/2013  00:58:07    01  80  0136    PRE UNIT ID

11/01/2013  00:58:16    01  80  0136    PRE UNIT ID

11/01/2013  00:58:22    01  00  0238    POST UNIT ID    

我想读取并在列表框中显示文本行,但 test.txt 是一个日志文件,并且每秒都会在此文本中插入一个新行。使用带有计时器的方法,相同的行正在重复。更新列表框以显示用文本文件写入的最新行的最佳解决方案是什么?我认为这是一种来自文本文件的更新......

Private Sub Form_Load()
    Timer1.Enabled = True
    Timer1.Interval = 1000

End Sub

Private Sub Timer1_Timer()

    filenum = FreeFile                      
    filepath = "C:\test.txt"

    Open filepath For Input As filenum       
    Do Until EOF(filenum)                   
        Line Input #filenum, LineText       
        List1.AddItem LineText         
    Loop                                    
    Close filenum

End Sub
4

1 回答 1

2

添加日志文件前清除列表框

Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 1000

End Sub
Private Sub Timer1_Timer()

filenum = FreeFile                      
filepath = "C:\test.txt"

            Open filepath For Input As filenum       
            List1.Clear
            Do Until EOF(filenum)                   
                Line Input #filenum, LineText       
                List1.AddItem LineText         
            Loop                                    
            Close filenum
End Sub
于 2013-11-01T15:25:31.873 回答