Vb.net 如何在winform上每N秒更新一次label控件?

Vb.net 如何在winform上每N秒更新一次label控件?,vb.net,winforms,Vb.net,Winforms,我的winform上有一个标签,当窗体加载时,它会统计目录中的文件数。我想在winform应用程序运行时每N秒更新一次该标签。我不知道如何做到这一点,这里是我的加载计数文件: Public Function getUserCountsTotal() As Integer Dim di As New DirectoryInfo("C:\myDirectory") Dim Users As FileInfo() = di.GetFiles().OrderByDescending(Fu

我的winform上有一个标签,当窗体加载时,它会统计目录中的文件数。我想在winform应用程序运行时每N秒更新一次该标签。我不知道如何做到这一点,这里是我的加载计数文件:

Public Function getUserCountsTotal() As Integer
    Dim di As New DirectoryInfo("C:\myDirectory")
    Dim Users As FileInfo() = di.GetFiles().OrderByDescending(Function(fi) fi.LastWriteTime).ToArray()

    Dim user As FileInfo

    'list the names of all files in the specified directory
    For Each user In Users
        ComboBox1.Items.Add(Path.GetFileNameWithoutExtension(user.Name) & "- " & user.LastWriteTime)
    Next

    getUserCountsTotal = ComboBox1.Items.Count + 1
End Function

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    statusPanel.Text = ""
    statusPanel.Text = "Logged in as " & getYourUserName() & " - " & "[ " & getUserCountsTotal() & " ]  "
End Sub

在窗体上放置计时器,并将
Enabled
属性设置为true。双击计时器并输入计数目录中文件的代码:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim counter = My.Computer.FileSystem.GetFiles("C:\myDirectory")
    Label1.Text = counter.Count
End Sub

您可以在计时器的
interval
属性中以毫秒为单位设置间隔。要每两秒钟更新一次,请将值设置为2000;要每三秒钟更新一次,请将其设置为3000等。

使用一个数字,因为您不断向组合框添加新项目,而不删除旧项目。您需要在开始添加新项目之前致电。