Adding Progress bar while loading Image list to listbox using vb.net
I am trying to add Images using open file dialog and folder browse dialog and populating the image list to listbox.While adding these Images I need to show the progress bar for every image it loads.
I am trying to get that but when the value of the progress bar say something around 25 0r 40 it is stopping at that point but I need to show the progress bar until it completes the 100% and then populates the Image list.
How do I do that?
Here is my code:
Private Sub AddImages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddImages.Click
If Not Directory.Exists(Application.StartupPath + "\Backup\") = True Then
Directory.CreateDirectory(Application.StartupPath + "\Backup\")
End If
OpenFileDialog1.FileName = "Select a Image"
OpenFileDialog1.Multiselect = True
OpenFileDialog1.InitialDirectory = "C:\Users\Public\Pictures\Sample Pictures"
OpenFileDialog1.Filter = "All Type Of Image Files|*.*|Joint Photographic Experts Group [JPEG]|*.jpg|Bitmap [BMP|*.bmp|Tagged Image File Format [TIFF]|*.tiff|Portable Network Graphics [PNG]|*.png"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
ProgressBar2.Show()
ProgressBar2.Step = 10
Dim str As String
For Each str In OpenFileDialog1.FileNames
Load.Text = "Loading..."
Dim fso As New FileSystemObject
Dim MyName As String
Dim MyExtension As String
开发者_C百科 MyName = fso.GetFileName(CStr(str))
MyExtension = fso.GetExtensionName(MyName)
System.IO.File.Copy(str, Application.StartupPath + "\Backup\" + MyName & "." & MyExtension, True)
CheckedListBox1.Items.Add(str, CheckState.Checked)
Thumbcontrol1.AddThumbnail(str)
Thumbcontrol1.BackgroundImage = Nothing
CheckedListBox1.SelectedIndex = 0
ProgressBar2.PerformStep()
Next
SaveProject.Enabled = True
Delete.Enabled = True
Edit.Enabled = True
ClearAll.Enabled = True
CheckAll.Enabled = True
UncheckAll.Enabled = True
Timer1.Stop()
Load.Text = "Loading Completed"
Else
End If
ProgressBar2.Visible = False
Load.Text = Nothing
End Sub
Remove ProgressBar2.Step = 10
line and do this:
Dim str As String
Dim counter As Integer 'new!
For Each str In OpenFileDialog1.FileNames
Load.Text = "Loading..."
Dim fso As New FileSystemObject
Dim MyName As String
Dim MyExtension As String
MyName = fso.GetFileName(CStr(str))
MyExtension = fso.GetExtensionName(MyName)
System.IO.File.Copy(str, Application.StartupPath + "\Backup\" + MyName & "." & MyExtension, True)
CheckedListBox1.Items.Add(str, CheckState.Checked)
Thumbcontrol1.AddThumbnail(str)
Thumbcontrol1.BackgroundImage = Nothing
counter += 1 'new
CheckedListBox1.SelectedIndex = 0
ProgressBar2.Value = (counter * 100) / OpenFileDialog1.FileNames.Length 'new
Next
It does not address the problem of your not using threading, as other answers are correct to point out.
Don´t use the UI for loading. Use Background worker. BackgroundWorker works in another thread, and it can report the progress to bind it to a progressbar.
The progress bar stops moving because Windows thinks there is something seriously wrong with your program. It replaces your main window with the 'ghost' window, you can tell because it says "Not Responding" in the title bar.
It does this to help alert the user that your program is dead to the world and will not respond to any input from the user. Clicking the mouse or banging on the keyboard will not have any effect, all the user can do is watch helplessly while your code goes through the motions.
This does not make a desirable user interface. You solve it by using BackgroundWorker so the heavy lifting is done on a separate thread. Leaving the user interface thread responsive. Be sure to read the MSDN article for it so you know what to do, you'll have to adapt your code.
精彩评论