【发布时间】: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()。