【问题标题】:How to update a label control every nTH seconds on winform?如何在winform上每nTH秒更新一次标签控件?
【发布时间】:2016-10-25 20:21:11
【问题描述】:

我的 winform 上有一个标签,当表单加载时,它会计算目录中有多少文件。我想在 winform 应用程序运行时每隔 nTH 秒更新一次该标签。我不知道如何实现这一点,这是我的加载文件:

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

【问题讨论】:

  • 使用Timer?
  • 该数字不断增加,因为您不断向ComboBox 添加新项目,而没有删除旧项目。在开始添加新项目之前,您需要致电 ComboBox.Items.Clear()

标签: vb.net winforms


【解决方案1】:

在表单上放置一个计时器并将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

您在 Timer 的 Interval 属性中设置间隔(以毫秒为单位)。要每两秒更新一次,请将值设置为 2000,每三秒更新一次,请将其设置为 3000,以此类推。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-14
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多