View Single Post
  #1 (permalink)  
Old November 26th, 2013, 11:00 PM
Wendel Wendel is offline
Authorized User
Points: 96, Level: 1
Points: 96, Level: 1 Points: 96, Level: 1 Points: 96, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2013
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson37TryIt_LINQtoObjects_AddProgressBar

Hi
Having completed lesson37, I tested all my new found creative skills by adding a “ProgressBar” to Lesson37TryIt with limited (0) success.
Can you elaborate on how to / where to:
1) add a counter (loop) on the number of files found – is it in //Search for files
2) sample code for “BackgroundWorker” and “ProgressBar” .
This is slightly out of scope, but any assistance would be appreciated.
My creative code (which produces a belated progress bar) is attached.
Regards
Wendel

Code:
namespace SearchForFiles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // Start at the startup directory.
        private void Form1_Load(object sender, EventArgs e)
        {
            directoryTextBox.Text = Application.StartupPath;
        }

        // Search for files matching the pattern
        // and containing the target string.
        public void searchButton_Click(object sender, EventArgs e)
        {
            //Start BackgroundWorker
            backgroundWorker1.RunWorkerAsync();
            
            //initialise the pickDaysComboBox
            int noDays = int.Parse(pickDaysComboBox.Text);

            // Get the file pattern and target string.
            string pattern = patternComboBox.Text;
            string target = targetTextBox.Text.ToLower();
            string writeDate = writeDateTimePicker.Text;

            // Search for files.
            DirectoryInfo dirinfo = 
                new DirectoryInfo(directoryTextBox.Text);
            var fileQuery =
                from FileInfo fileinfo
                in dirinfo.GetFiles(pattern, SearchOption.AllDirectories)
                where
                 (File.ReadAllText(fileinfo.FullName).ToLower().Contains(target) &&
                 (fileinfo.LastWriteTime.Subtract(DateTime.Now).TotalDays >= -noDays))
select (fileinfo.Length.ToString() + "\t" + fileinfo.FullName + "\t" +     fileinfo.LastWriteTime);

            //Display result
            fileListBox.DataSource = fileQuery.ToArray();
            int countLBItems = fileListBox.Items.Count;
            this.Text = ("Number of files found: " + countLBItems);
            MessageBox.Show("Done");
        }

        private void openDirButton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                directoryTextBox.Text = folderBrowserDialog1.SelectedPath;
        }

        public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //Loop counter
            int countLBItems = fileListBox.Items.Count;
            for (int j = 0; j < countLBItems -1; j++)
            {
                j++;
                backgroundWorker1.ReportProgress(j);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //Change the value of the progressBar to the BackgroundWorker progress
            progressBar1.Value = e.ProgressPercentage;
        }
    }
}