Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a question. I would like to update a ProgressBar from another form.

My project works like this:

Form1 - Calls Form2 (with Form2.Show)
Form2 - Makes some work and has to update my ProgressBar in Form3
Form3 - Here is my ProgressBar


I have tried the following:

In Form3:
C#
public int ProgressBarValue
{
    set { this.progressBar_Status.Value = value; }
}
public int IncrementProgessBar
{
    set { progressBar_Status.Increment(value);
}

public int ProgressBarMaximum
{
    set { ConfigProgressBar(value); }
}

private void ConfigProgressBar(int intMax)
{
    this.progressBar_Status.Visible = true;
    this.progressBar_Status.Minimum = 0;
    this.progressBar_Status.Maximum = intMax;
    this.progressBar_Status.Value = 1;
    this.progressBar_Status.Step = 1;
}


In Form2:
C#
public Form3 myOtherForm = new Form3();

public doWork()
{
     foreach (Element zenVar in lstAlarms)
     {
         //Increment ToolStrip
          this.myOtherForm.IncrementProgessBar = 1;
                    
          //my work
      }
}



This does not work! I don't know why.
Can you tell me where am I wrong? or could you tell me which other way there is to accomplish this task?

Thank you.
Cis
Posted
Updated 26-Nov-14 20:35pm
v2
Comments
BillWoodruff 25-Nov-14 9:53am    
This is not a good design choice: put the ProgressBar on either the main Form or the second Form, not another Form.
cista 27-Nov-14 1:59am    
I need to do it like that! Because I am using the Form 1 as Mdi container with WeifenLuo.WinFormsUI.Docking!

Is there any other way to accomplish what I need?
Marcin Kozub 2-Dec-14 6:48am    
Can you explain why any of solutions is not working? What is wrong? Do you have any error messages?
cista 2-Dec-14 7:19am    
I solved it with an EventHandler, see my post below!
Marcin Kozub 2-Dec-14 7:34am    
OK, solution wasn't accepted then...

You're trying to call IncrementProgressBar from form3 by form2 object (myOtherForm)

You should have form3 instance and call its update method (hopefully, form3 will be visible somewhere or there is no point)
 
Share this answer
 
Comments
cista 27-Nov-14 2:10am    
The Form1 is used as a Mdi container with WeifenLuo.WinFormsUI.Docking, so I am showing there the Form2 and Form3.

You mean to call the update method in Form3 when I do an increment? oder when?

If it is like that, I already tried that like this and it did not work :( :
<pre lang="c#">public int IncrementProgessBar
{
set { progressBar_Status.Increment(value);
this.Refresh();
this.Update();
}
}</pre>
Sinisa Hajnal 27-Nov-14 2:29am    
What I meant is that in your code description, you say you have incrementprogressbar as method in Form3. And yet, you're calling it on Form2 object. In Form2 object (myOtherForm) you should have a reference to Form3 object and update it. Also, in update try calling Invalidate after changing progress bar value. And finally, do you know for sure that the method gets called and the value incremented? Set a breakpoint and check.

Thank you.
cista 27-Nov-14 2:34am    
Oh that was a type error. I meant, what I have is
public Form3 myOtherForm = new Form3();
So myOtherForm is instanced to Form3.

The method gets called and the values of the progressbar in Form3 also changes, but I cannot see the progressbar itself incrementing in the form.

What do you mean by Invalidate?
Sinisa Hajnal 27-Nov-14 3:10am    
Literally. There is invalidate function on the form object (actually on any UI control) which forces it to redraw. This is common problem with background forms, you update something, but unless the screen redraws for some reason (like resize or movement of the form(s)) the change will not be shown.
Hello,

Quote:
The Form1 is used as a Mdi container with WeifenLuo.WinFormsUI.Docking, so I am showing there the Form2 and Form3.


You are doing it wrong. You are creating both Form2 and Form3 instances in Form1 (MDI). Then in Form2 you are creating another instance of Form3. It's completely wrong.

I suppose that Form3 is just form with progress bar control only to show operation progress to the user and there is no other use of this form.

1. In Form1 you need to create and show Form2 only:
C#
var form2 = new Form2();
form2.Show();


2. Then create Form3 inside Form2:
C#
private Form3 myOtherForm = new Form3();

public doWork()
{
     this.myOtherForm.Show();
     foreach (Element zenVar in lstAlarms)
     {
         //Increment ToolStrip
          this.myOtherForm.IncrementProgessBar = 1;
                    
          //my work
      }
      this.myOtherForm.Hide();
}


Second solution is to create Form2 and Form3 in Form1 and then pass reference to created Form3 instance directly to Form2 via constructor.

But in my opinion both solutions are a bad design. For simple, home project with 4-5 forms it can be 'acceptable' but for medium/large size projects you should consider using some Design Patterns. For instance:
1. Model-View-Presenter - which can help you separate your app layers
2. Create some kind AppHost/Shell to manage object communication, especially when you're dealing with many forms and want to pass data between them.
3. And most important use S.O.L.I.D. programming :)
You can find many articles about SOLID and Design Patterns here on CP. Good Luck.

I hope that help you.
 
Share this answer
 
v2
Hi,
I resolved it with EventHandler, like this:

Form3
C#
public Form3()
{
    InitializeComponent();
    Form.UpdateProgressBar += new ProgressBarEventHandler(OnProgressBarUpdate);
}
public void OnProgressBarUpdate(object sender, ProgressBarEventArgs e)
{
    if (e.ProgressBarIncrement != 0)
        progressBar_Status.Increment(e.ProgressBarIncrement );
}

public delegate void ProgressBarEventHandler(object sender, ProgressBarEventArgs e);
public class ProgressBarEventArgs
{
        public ProgressBarEventArgs(int intIncrement)
        {
            ProgressBarIncrement = intIncrement;
        }
        public int ProgressBarIncrement { get; private set; }
}


In Form2:
C#
public static event ProgressBarEventHandler UpdateProgressBar;
public doWork()
{
     foreach (Element zenVar in lstAlarms)
     {
         //Increment Progressbar
         UpdateProgressBar(this, new ProgressBarEventArgs(0, 1, false));
                    
          //my work
      }
}
 
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