2016-10-25 8 views
-3

フォームが読み込まれたときに、ディレクトリ内にいくつのファイルがあるかを数えます。 winformアプリケーションの実行中にn秒ごとにそのラベルを更新したい。私はこれをacheiveするかどうかはわかりません、ここにファイルをカウントする負荷に私のです: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

[Timer?](https://msdn.microsoft.com/en-us/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プロパティで間隔を設定します。 2秒ごとに更新するには、値を2000に設定し、3秒ごとに更新して3000などに設定します。

関連する問題