?
Solved

A counter for winform in vb.net 2005

Posted on 2007-10-09
6
Medium Priority
?
712 Views
Last Modified: 2013-11-26
I have the code below and would like to add a counter so I can display the total number of files found. Any help would be appreciated. Also is  there a way to have some type of animation while it is searching for results?

Public Class Form1
    Private dt As New DataTable()
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        Me.DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
        Me.DataGridView1.AutoSize = True
        dt.Columns.Add("FileName", GetType(String))
        dt.Columns.Add("Extension", GetType(String))
        dt.Columns.Add("Directory", GetType(String))
        dt.Columns.Add("Modified Date", GetType(DateTime))
    End Sub
    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        dt.Rows.Clear()
        Dim strDir As String
        strDir = "Q:\"
        DirAndFilesViewer(New System.IO.DirectoryInfo(strDir))
        DataGridView1.DataSource = dt
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
    Sub DirAndFilesViewer(ByVal di As System.IO.DirectoryInfo)
        For Each fi As System.IO.FileInfo In di.GetFiles
            Dim targetDate As Date
            If Last10.Checked Then
                targetDate = Date.Today.AddDays(-10)
                If fi.LastWriteTime >= targetDate Then
                    If UCase(fi.Extension) = ".TXT" Or UCase(fi.Extension) = ".ASP" Or UCase(fi.Extension) = ".ASPX" Or UCase(fi.Extension) = ".HTM" Or UCase(fi.Extension) = ".HTML" Then
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
                    End If
                End If
            End If
            If LastDay.Checked Then
                targetDate = Date.Today.AddDays(-1)
                If fi.LastWriteTime >= targetDate Then
                    If UCase(fi.Extension) = ".TXT" Or UCase(fi.Extension) = ".ASP" Or UCase(fi.Extension) = ".ASPX" Or UCase(fi.Extension) = ".HTM" Or UCase(fi.Extension) = ".HTML" Then
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
                    End If
                End If
            End If
        Next

        For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
            DirAndFilesViewer(subDi)
        Next
    End Sub
End Class
0
Comment
Question by:-cr-
6 Comments
 
LVL 4

Accepted Solution

by:
jimstar earned 2000 total points
ID: 20041024
You could change your DirAndFilesViewer to a function and return the total number of files added:

Function DirAndFilesViewer(ByVal di As System.IO.DirectoryInfo) as Integer
Dim count as Integer
Count = 0
        For Each fi As System.IO.FileInfo In di.GetFiles
            Dim targetDate As Date
            If Last10.Checked Then
                targetDate = Date.Today.AddDays(-10)
                If fi.LastWriteTime >= targetDate Then
                    If UCase(fi.Extension) = ".TXT" Or UCase(fi.Extension) = ".ASP" Or UCase(fi.Extension) = ".ASPX" Or UCase(fi.Extension) = ".HTM" Or UCase(fi.Extension) = ".HTML" Then
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
            Count = count + 1

                    End If
                End If
            End If
            If LastDay.Checked Then
                targetDate = Date.Today.AddDays(-1)
                If fi.LastWriteTime >= targetDate Then
                    If UCase(fi.Extension) = ".TXT" Or UCase(fi.Extension) = ".ASP" Or UCase(fi.Extension) = ".ASPX" Or UCase(fi.Extension) = ".HTM" Or UCase(fi.Extension) = ".HTML" Then
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
            Count = count + 1

                    End If
                End If
            End If
        Next

        For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
            count = count + DirAndFilesViewer(subDi)
        Next

   DirAndFilesViewer = count
    End Function
0
 

Author Comment

by:-cr-
ID: 20041105
Great the counter works! Any suggestion on how to display an animation during the search?
0
 
LVL 4

Expert Comment

by:jimstar
ID: 20041186
Look into the progress bar control in the toolbox. You can drag it onto your form and hide it, then show it before you start searching for files.

The problem is knowing how many files you will have total, which you need in order to set the percent complete value on the progress bar. For example, ProgressBar1.value = current_files / total_files.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 18

Expert Comment

by:Priest04
ID: 20041845
You can:

1) to have progress bar showing percentafe, you need to first loop through all files to get the number of files, so you can implement progress part showing percentage of the completed task, or

2) set ProgressBar.Style = ProgressBarStyle.Marquee, so it will loop continuously, until the job is done.

Goran
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 20042068
You can simplify your code a bit to:

        ...
            Dim targetDate As Date
            If Last10.Checked Then
                targetDate = Date.Today.AddDays(-10)
            ElseIf LastDay.Checked Then
                targetDate = Date.Today.AddDays(-1)
            End If
            If fi.LastWriteTime >= targetDate Then
                Select Case UCase(fi.Extension)
                    Case ".TXT", ".ASP", ".ASPX", ".HTM", ".HTML"
                        dt.Rows.Add(New Object() {fi.Name, fi.Extension, fi.DirectoryName, fi.LastWriteTime})
                        Count = count + 1

                End Select
            End If
0
 

Author Comment

by:-cr-
ID: 20043697
Idle_Mind,
Thanks for the advice, no need for redundancy.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question