I have created one windows form, there i have a progress bar, i want to update the value of progress bar, from another class. I have written one function in my class on its execution, i want to see the progress in progress bar.
For Example:
namespace UpdateProgressBar
{
public partial class ProgressBarUdpate : Form
{
public ProgressBarUdpate()
{
InitializeComponent();
}
private void btn_Submit_Click(object sender, EventArgs e)
{
UpdateDataProgress updt = new UpdateDataProgress();
updt.ExecuteFucntion();
}
}
}
namespace UpdateProgress
{
public class UpdateDataProgress
{
public void ExecuteFucntion()
{
for (int i = 0; i < 100; i++)
{
}
}
}
}
My Expected output is when i call updt.ExecuteFucntion function it should update the progressbar values as per loop implemented in another class.
You should use Event
in such requirement.
Logic:
As your basic requirement is to update Progressbar
( UI
) based on current status of execution of a method (which is in class library)
You need to raise an event while executing ExecuteFucntion()
. This Event will be Handled at ProgressBarUdpate
form.
As you can see in code below, after creating an object of UpdateDataProgress
, we are subscribing it's event by updt.ExecutionDone += updt_ExecutionDone;
Thus as soon as That event raised form ExecuteFucntion()
it will call updt_ExecutionDone
of ProgressBarUdpate
where you can suerly update your progress bar.
Update your code as below.
public partial class ProgressBarUdpate : Form
{
public ProgressBarUdpate()
{
InitializeComponent();
}
private void btn_Submit_Click(object sender, EventArgs e)
{
UpdateDataProgress updt = new UpdateDataProgress();
updt.ExecutionDone += updt_ExecutionDone;
updt.ExecuteFucntion();
}
void updt_ExecutionDone(int value)
{
//Update your progress bar here as per value
}
}
and class UpdateProgress
public delegate void ExceutionHandler(int value);
public class UpdateDataProgress
{
public event ExceutionHandler ExecutionDone;
public void ExecuteFucntion()
{
for (int i = 0; i < 100; i++)
{
//perform your logic
//raise an event which will have current i
// to indicate current state of execution
// use this event to update progress bar
if (ExecutionDone != null)
ExecutionDone(i);
}
}
}
You can use Events, or simply:
Code in form:
private void btn_Submit_Click(object sender, EventArgs e) { UpdateProgress.UpdateDataProgress updt = new UpdateProgress.UpdateDataProgress(); updt.ExecuteFucntion(progressBar1); }
Code in class:
public class UpdateDataProgress { public void ExecuteFucntion(System.Windows.Forms.ProgressBar progbar) { for (int i = 0; i < 100; i++) { progbar.Value = i; } } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.