Read Text Files in a folder in Label at 5 seconds interval

Wilfred Eghenedji 326 Reputation points
2022-04-01T00:55:41.333+00:00

I am intending to access a folder that contains text files. I would like read each text file through a label, displaying each at 5 seconds interval. i have tried using Label18.Text = IO.File.ReadAllText("ph1\testing.txt") but only reads a single file. Don't know how to expand this or do something else to achieve my objective. Thank you.

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-04-01T13:23:18.49+00:00

    Hi
    Here is a simplified example.

    ' Form1 with Label1 and Timer1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim files() As String
        Dim Pause As Integer = 5
        Dim pointer As Integer = 0
        ' set to required folder
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.Desktop
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            files = IO.Directory.GetFiles(folder, "*.txt")
            Timer1.Enabled = True
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Timer1.Interval = Pause * 1000
            Label1.Text = IO.File.ReadAllText(files(pointer))
            pointer += 1
            If pointer > files.Count - 1 Then pointer = 0
        End Sub
    End Class
    

1 additional answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-04-01T01:08:48.573+00:00

    i have tried using Label18.Text = IO.File.ReadAllText("ph1\testing.txt") but only reads a single file.

    Well sure, you need to get a list of file names to read.

    https://www.bing.com/search?q=vb.net+get+a+list+of+files+in+a+folder

    Then you need to sleep for the desired 5 seconds.

    https://www.bing.com/search?q=vb.net%20sleep%20for%205%20seconds

    There are plenty of sites on the internet that provide examples. If you have no idea what you are doing, then I would recommend taking programming courses at a local community school where you can gain the necessary experience.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.