Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi
I am use progress bar in one form,if i call progress bar form another form,need to show progress bar with animation,like timer start and show progressing in progress bar.How to start timer and show progress bar in another form.

E.g.I have form1 and form2,in form1 i have one button,in form2 i have progress bar with timer start and progressing code like below

VB
Public Class ProgressBar
    Private ticks As Integer = 0
    Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
        ticks += 1
        ProgressBar1.Value1 = ticks
        If ticks = 100 Then
             ticks = 0
        End If
    End Sub

    Private Sub ProgressBar_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Timer1.Enabled = False
    End Sub

    Private Sub ProgressBar_Load(sender As Object, e As EventArgs) Handles Me.Load
        Timer1.Enabled = True
    End Sub
End Class


In above code i will start progress,once timer ticks reach 100 aging i restart progress,here once form2 start ,i start progress animation and once form2, close i stop progress bar animation.Here i start form2 page directly it work fine,but if i call form2 from form1 button click event it will not show progress bar animation,simply it appear without progress animation.

Below is form1 button click event.

VB
Dim proBar As New ProgressBar
proBar.Show()


Why if open form2 directly it work,if call form another page it not work ?

Note:In form1 button click event i have some other process,i.e i will load lot of nodes(more than 10000 nodes) in treeview,it take more time and it hang few mins,so here i need to call progress bar,so once start load nodes in treeview i need to call progress bar and once finish need to close progress bar.

Pls reply asap

Regards
Aravind
Posted
Updated 22-Oct-14 20:14pm
v2
Comments
Maciej Los 23-Oct-14 4:35am    
What you mean by: "Why if open form2 directly it work,if call form another page it not work ?"
Aravindba 23-Oct-14 21:07pm    
Yes it not work,if i use form2.showdialog() then it work,but how to close form2 ?bcz here i am not going to show any message and button for click close for form2,just i need show one progress bar and close.
[no name] 23-Oct-14 20:26pm    
Why are you not using the Progressbars EventArgs to get the value and stop the timer accordingly?

Also, how did you declare your timer1? Did you drag it on to the form from the toolbox or did you declare it in code? If you are trying to call another subroutine from another form, you will need to look at using a delegate to invoke the desired control on that other forms thread.

For an example to my comment regarding Progress bars EventArgs, you could also do something like this:

VB
'Declare the event as so...
Public Event ProgressChanged As ProgressChangedEventHandler

Then look at Google to Raise An Event from another form, or this SO Question for a multithreaded example.
VB
' This event handler updates the progress bar accordingly.
Private Sub backgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged
    Me.progressBar1.Value = e.ProgressPercentage
End Sub

The example makes use of a background worker.
 
Share this answer
 
Comments
Aravindba 23-Oct-14 21:15pm    
Thank you for ur reply,actually i will bind node in treeview when click parent node,that time some child node have lot of sub folders some node not have,so we dont know exact time for bind node in treeview,so before start i will call progressbar and once finish i will close progress,currently i will show "Please Wait..." word using lable in form2,so when start i will show form2 and after finish i will close form2.if here show some progressbar with loading animation like how windows xp screen show loading bar like that.

And one thing when click parent node in treeview that load its sub nodes,this is better way,bcz at page load if we bind all node in treeview,it take 20 to 40 mins,sometime it hang and show "Not Responding" message.
[no name] 23-Oct-14 21:31pm    
You're welcome and I hope the suggestion helped you. If it did, please accept it as a solution. And If you have any other questions, I will be happy to help you.

That is a very long time to wait for some bindings. May I ask, what exactly is it you are binding? Are they folders and files or something?
Aravindba 23-Oct-14 22:04pm    
Yes,folder and files,that one not form system or drive,we are develop document capture system user upload files in its account(i.e save in sql server according to user name),more than 10000 users and each user have 10 sub folder ,and each 10 sub folders have lot of folder and files .
[no name] 23-Oct-14 22:08pm    
Well given the information you have given me, the background worker is what you should be using to handle the process of the information being transferred as well as updating your progress bar. Further, you can do all your updating in the DoWork Event.


On that note; would you kindly either rate my answer, or accept it as a solution. Thanks
Aravindba 23-Oct-14 23:48pm    
Can u explain me ? how above in works ? backgroundWorker1 ,this is what ? Form or progress bar property ? and if handle above code in same page where i bind treeview and progress bar also in same page ?

If i put progress bar in same page and i try above code ? progress bar nothing show loading animation.i mean Form1 as my page and i add code like in Form1 _ProgressChanged,it nothing trigger this event,then how add value to progress bar.
Hi Aravind,

Regarding this - "In form1 button click event i have some other process,i.e i will load lot of nodes(more than 10000 nodes) in treeview,it take more time and it hang few mins,so here i need to call progress bar,so once start load nodes in treeview i need to call progress bar and once finish need to close progress bar."

You are using a System.Windows.Forms.Timer. The timer cycle will be blocked when the UI is blocked. the UI is block when you run your long process to create the 1000+ treeview nodes(i.e you cannot click or inter-react with any GUI element). Use a System.Timers.timer class for the timer. This is non blocking on the UI thread. You will have to handle anyUIControl.invokeRequired()
so that making changes to the progressBar will not throw a cross thread exception.

regs. Ron O.
 
Share this answer
 
Comments
Aravindba 6-Nov-14 2:40am    
Hi How i use System.Timers.Timer,in this code ? here i use windows timer to show progress bar,
Imports Telerik.WinControls
Imports System.ComponentModel

Public Class ProgressBar
Private ticks As Integer = 0
Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
ticks += 1
RadProgressBar1.Value1 = ticks
If ticks = 100 Then
' Timer1.Enabled = False
ticks = 0
End If
End Sub

Private Sub ProgressBar_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Timer1.Enabled = False
End Sub

Private Sub ProgressBar_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Enabled = True
End Sub
End Class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900