Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having a form with label and button control. and a class having TestMethod
contain a for loop from 0 to 9 and when the method is called from button click,
I want to print the loop counter value on that label i.e. 0 of 9 , 1 of 9 and so on.

I want to achieve that scenrio.

Thanks in advance...

What I have tried:

I tried using an event handler and delegate but not able to get the exact logic.
Posted
Updated 11-May-16 19:42pm
Comments
iamcpmember 12-May-16 1:09am    
Please write your tried code here alongwith your question...
Sergey Alexandrovich Kryukov 12-May-16 1:42am    
You handle the event Click using += operator. On it's right, you have to supply a delegate instance of appropriate type.
If this is unclear, learn delegates and events from the very beginning. You won't be able to do any software development without learning it all anyway.
—SA
BillWoodruff 12-May-16 4:49am    
Unless you give further details, I don't think we can assist you. Is your intent to use the Label as a kind of "progress report" ? If so, then you need to do something to pause between each value shown since the loop will exit in milliseconds, and only the final value of the loop counter will be shown on the label.

Are you required to deal with threading here ?

Setting up the delegate is simple:
C#
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:
C#
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:
C#
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:
C#
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!
 
Share this answer
 
Comments
Karthik_Mahalingam 12-May-16 2:18am    
5, Excellent
try this

C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           MyClass obj = new MyClass();
           obj.TestMethod(this.lblCount);
       }


   }

   public class MyClass
   {
       public void TestMethod(Label lbl)
       {
           for (int i = 0; i <= 9; i++)
           {
               System.Threading.Thread.Sleep(100); // delay to see the visual of label value changing
               lbl.Text = string.Format("{0} of 9", i);
               Application.DoEvents(); // to reflect label change value on form
           }
       }
   }


I have done in simpler way of achieving your task without using events and delegates,
Please refer OrginalGriff's Solution for the neat explanation.
 
Share this answer
 
v2

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