How to update progress bar from other class?
Create a public property or a public method in the form containing the progress bar
public void SetProgress(int progress)
{
progressBar.Progress = progress;
}
Now you can update the progress bar with
myForm.SetProgress(50);
Another approach is to have a ProgressChanged
event somewhere and let the form subscribe to this event.
public class Tool {
public event Action<int> ProgressChanged;
private void OnProgressChanged(int progress)
{
var eh = ProgressChanged;
if (eh != null) {
eh(progress);
}
}
public void DoSomething()
{
...
OnProgressChanged(30);
...
}
}
In the form you would have something like this
private Tool _tool;
public MyForm () // Constructor of your form
{
_tool = new Tool();
_tool.ProgressChanged += Tool_ProgressChanged;
}
private void Tool_ProgressChanged(int progress)
{
progressBar.Progress = progress;
}