Question

I want to add a textbox to a program which lists files in given path. It will show current searching directory on a panel. even if I added textvox1.Text = dir like this, It only shows last directory at the end of the search. Can you tell me why it doesn't work? Related question

public void GetFiles(string dir)
{
    textBox1.Text = dir;
    string[] filetypes = new string[] { "cfg", "txt" };
    foreach (string ft in filetypes)
    {                
        foreach (string file in Directory.GetFiles(dir, string.Format("*.{0}", ft), SearchOption.TopDirectoryOnly))
        {                   
            files.Add(new FileInfo(file));
        }                
    }
    foreach (string subDir in Directory.GetDirectories(dir))
    {                
        try
        {                    
            GetFiles(subDir);
        }                    
        catch
        {
        }            
    }
}
Was it helpful?

Solution

The "quick fix" is to call DoEvents() after setting the TextBox:

textBox1.Text = dir;
Application.DoEvents();
// ... rest of the code ...

The proper fix, however, is to place that code into a background thread. If you use the BackgroundWorker() control you can use ReportProgress() and the associated ProgressChanged() event to update the TextBox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top