简体   繁体   中英

How to display name on progressbar in vb.net

In my vb.net winform application, writing a file from one directory to another. I have the progressbar1 to show the status of copy. Now I want to display the current filename that is copying on progressbar1.

Any suggestions please?

ConfigWindow.FileProgressBar.Visible = True
Dim fileLines() As String = System.IO.File.ReadAllLines("C:\Desktop\Demo.csv")
ConfigWindow.FileProgressBar.Maximum = fileLines.Count + 1
ConfigWindow.FileProgressBar.Minimum = 0
ConfigWindow.FileProgressBar.Value = 0

For index As Integer = ProgressBar1.Minimum To ProgressBar1.Maximum

    ConfigWindow.FileProgressBar.Value = index
    ConfigWindow.FileProgressBar.PerformStep()

Next

During file copy progressbar is moving till the end and it is working absolutely fine. Onle concern is displaying filename on progressbar.

Cody's suggestion about using a BackgroundWorker to update your UI as you copy files is correct.

However, if you're trying to display text over a standard Winform ProgressBar you'll be disappointed to learn you can't do this with just the standard ProgressBar alone.

Over at codeproject.com there are a couple custom progressbar controls you can look at:

Custom Control for Text Over a Progress Bar
Custom ProgressBar Control

I know VB.NET is your preference. One of these controls is written in C# but is packaged in a dll so you can use it in your VB.NET project as you would any other dll.

Of course another option would be to use a StatusStrip on your form with a cell for a progress bar and another for text displaying the filename being copied.

Presumably, your problem is that the status bar never gets updated while the file copy operation is in progress, which means you can't see the names of individual files being copied.

That's a classic symptom of doing work on the UI thread. You're blocking the ability of the status bar to update itself. There are zillions of questions here on Stack Overflow about that already. The simple answer is to move the file copy operation to a BackgroundWorker , which periodically reports its progress. From those progress reports, captured on the UI thread, you can update the status bar.

The above-linked MSDN documentation has a great sample already written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM