2016-10-25 109 views
-3

我的winform上有一個標籤,當表單加載時,計算一個目錄中有多少個文件。我想在winform應用程序運行時每隔n秒更新一次該標籤。我不知道如何達致這,這裏的一對負載我來算的文件:如何在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 
+4

使用[定時器?](https://msdn.microsoft.com/zh-cn/library/system。 windows.forms.timer(v = vs.110).aspx) –

+1

這個數字總是加起來,因爲你不停地向'ComboBox'添加新的項目而不刪除舊的項目。你需要調用[**'ComboBox.Items.Clear()'**](https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.clear(v = vs.110).aspx),然後再開始添加新項目。 –

回答

0

把窗體上的計時器和Enabled屬性設置爲。計時器雙擊,進入計數在目錄中的文件代碼:

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等。