Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I make that progress bar from Form1 open Form2 when progress bar is full
Thanks
Posted
Updated 10-Mar-15 5:23am
v2
Comments
OriginalGriff 10-Mar-15 11:06am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.

You shouldn't make the Progress Bar Control open Form2, if that is what you mean. It's not its responsibility - a Progress Bar shows progress, nothing else. Consider you want to replace the Progress Bar by something else sometime then you would also have to re-implement the opening of Form2 (maybe you wouldn't replace it ever but take it as an explanation why you should think about separation of concerns).

Instead:

You obviously have some lengthy process in your application, which is why you're showing the Progress Bar. At the point in the code where that process is finished, insert (or better: trigger *) the code for closing/hiding Form1 and/or the Progress Bar and opening Form2.

* : with an event + event handler

edit: typo
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Mar-15 21:50pm    
Very good explanation, especially in the part related to re-implementation, my 5.
I continued your post with some clarification and threading aspect of this; please see Solution 2.
—SA
[no name] 11-Mar-15 7:59am    
Thank you! 5ed yours as well.
In addition to Solution 1.

Even though triggering the window operation by a UI event is quite possible, it means logically wrong routing of events, mixing up causes and consequences. Progress bar, if used according to its intended design, really means some lengthy operation; technically it should mean that you have some non-UI thread doing its job. Let's denote this thread and all events on this thread (such as update of its progress percentage and completion) as "Th". Let's consider cause-consequence graph between the events in question:
Bad:
Th ────> progress bar ─────> show form

Good:
Th ────> progress bar
│
└──────> show form

Solution 1 perfectly explains why the case I depicted as "Bad" is really bad. More generally, you try to technically express the behavior of the application in a way not following its logic. Even if the results are formally equivalent, it is translated into considerable hardship in maintenance.

Now, how the non-UI thread should notify UI to make is showing a window (using Form.Show) and updating the ProgressBar.Value? You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

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