Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you run a progress bar from form 1 when form 2 is loading?

//Form1


C#
private void newEntriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Forms.SuppliesAvailability F = new SuppliesAvailability();
            F.MdiParent = this;
            F.Show();
        }

//When I clicked newEntriesToolStripMenuItem_Click form 1 must run this code while loading the form "backgroundWorker1.RunWorkerAsync();"

        public void Main_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        public void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Change the value of the ProgressBar to the BackgroundWorker progress.
            progressBar1.Value = e.ProgressPercentage;
            // Set the text.
            toolStripStatusLabel2.Text = e.ProgressPercentage.ToString();
        }

        public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 100; i++)
            {
                // Wait 100 milliseconds.
                Thread.Sleep(10);
                // Report progress.
                backgroundWorker1.ReportProgress(i);
            }
        }</pre>
Posted
Comments
BillWoodruff 27-Nov-14 23:01pm    
Is 'newEntriesToolStripMenuItem in the 'Main Form ? Why do you wish to show the ProgressBar on the 'Main Form rather than the 'SuppliesAvailability Form ? Where's the code for ' backgroundWorker1.ReportProgress(i) ?
V. 28-Nov-14 8:30am    
IMHO this doesn't make sense. Why would you like to run a counter when form 2 loads in order to show a progress bar? If form 2 needs to do some work on load time, let form 2 generate the progress parameters and use delegates/events to pass this information back to form 1 which in its turn will update the progress bar.

Hope this helps.
Member 10124914 30-Nov-14 19:16pm    
Because I have an MDI Form (Form1). Everytime a child form is loading, I want show a progress bar in the main form or form1 every time there is a loading child form.

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