Setting up the delegate is simple:
public class MyTestClass
{
public delegate void ShowProgress(int value);
public ShowProgress ShowProgressValue = null;
public void TestMethod()
{
for (int i = 0; i < 10; i++)
{
if (ShowProgressValue != null)
{
ShowProgressValue(i);
}
}
}
}
And using it is even simpler:
private void butTest_Click(object sender, EventArgs e)
{
MyTestClass mtc = new MyTestClass();
mtc.ShowProgressValue = GetProgress;
mtc.TestMethod();
}
private void GetProgress(int i)
{
Console.WriteLine(i);
}
But...to a label? That's a lot more complex. You could just replace the Console.WriteLine:
private void GetProgress(int i)
{
labResults.Text = i.ToString();
}
But all you will see is "9" - you will never see the intermediate values!
There are ways to do that - but the proper ways involve threading, and Invoking controls and it gets complicated.
You can bodge it:
private void GetProgress(int i)
{
labResults.Text = i.ToString();
Application.DoEvents();
Thread.Sleep(200);
}
But it's a bad idea, for reasons that are a bit complicated to explain at your skill level - your tutor will explain later when you get to threading and multithreaded apps.
An event based solution has the same problem! :laugh:
Unless you need to do this for homework, I'd find a better exercise to play with - threading is not trivial, and is best explained in a structured way!