代表和活动?

时间:2010-06-23 12:07:45

标签: c# delegates

这个问题与c#有关。方案是当我点击按钮时,文件读取,数据操作和文件转储等操作将会发生。每次操作完成后,我将更新状态(即,文件读取完成,数据操作完成)在UI中的标签(FORM-frmTesting)

按钮点击事件是

namespace frmTesting
      {
         public partial class Form1 : Form
         {
              private void button1_Click_1(object sender, EventArgs e)
              {
                 class1 l_objClass1 = new class1();
                  l_objClass1.DoOperation();

              }
          }


       public class class1
       {

            public int DoOperation()
            {
                  ReadTextFile();
                  ParsingData();
                  SaveTextFile();
                  return 0;
            }

            private int ReadTextFile()
            {
               //Read the text File
                return 0;

            }

            private int ParsingData()
            {
               // Data manipulation
                return 0;

            }

            private int SaveTextFile()
            {

               // save the file
               return 0;
            }
       }
   }

是否可以通过使用代理和事件....如果您有任何疑问,请回复我

2 个答案:

答案 0 :(得分:2)

您必须修改class1以广播其他类可以侦听的事件:

public class class1
{
    // Not necessary, but will allow you to add custom EventArgs later
    public delegate void StatusChangedEventHandler(object sender, EventArgs e);

    public event StatusChangedEventHandler FileRead;
    public event StatusChangedEventHandler FileParsed;
    public event StatusChangedEventHandler FileSaved;

    public int DoOperation()    
    {    
        ReadTextFile();    
        ParsingData();    
        SaveTextFile();    
        return 0;    
    }    

    private int ReadTextFile()    
    {    
        //Read the text File
        OnFileRead(EventArgs.Empty);   
        return 0;
    }    

    private int ParsingData()    
    {    
        // Data manipulation
        OnFileParsed(EventArgs.Empty);
        return 0;       
    }    

    private int SaveTextFile()    
    {      
        // save the file
        OnFileSaved(EventArgs.Empty);
        return 0;    
    }

    protected virtual void OnFileRead(EventArgs e)
    {
        if(FileRead != null)
            FileRead(this, e);
    }

    protected virtual void OnFileParsed(EventArgs e)
    {
        if(FileParsed != null)
            FileParsed(this, e);
    }

    protected virtual void OnFileSaved(EventArgs e)
    {
        if(FileSaved != null)
            FileSaved(this, e);
    }
}

然后让您的表单听取这些事件并适当更改其标签:

public partial class Form1 : Form 
{ 
    private void button1_Click_1(object sender, EventArgs e) 
    { 
        class1 l_objClass1 = new class1();

        l_objClass1.FileRead += 
            delegate { lblStatus.Text = "File Read..."; };

        l_objClass1.FileParsed += 
            delegate { lblStatus.Text = "File Parsed..."; };

        l_objClass1.FileSaved += 
            delegate { lblStatus.Text = "File Saved..."; };

        l_objClass1.DoOperation(); 
    } 
} 

答案 1 :(得分:0)

简短的回答是肯定的。您将事件添加到class1并使用适当的逻辑向Form1添加处理程序。以下是如何执行此操作的示例

public partial class Form1 : Form
    {


        private void button1_Click_1(object sender, EventArgs e)
        {
            class1 obj = new class1();
            obj.FileReadingComplete += HandleFileReadingComplete;
            obj.DataManipulationComplete += HandleDataManipulationComplete;

            obj.DoOperation();

            obj.FileReadingComplete -= HandleFileReadingComplete;
            obj.DataManipulationComplete -= HandleDataManipulationComplete;

        }

        private void HandleFileReadingComplete(object sender, EventArgs args){ 
            //code here
        }

        private void HandleDataManipulationComplete(object sender, EventArgs args)
        {
            //code here
        }

    }


    public class class1
    {

        public event EventHandler FileReadingComplete;
        public event EventHandler DataManipulationComplete;

        public int DoOperation()
        {
            ReadTextFile();
        OnFileReadingComplete();
        ParsingData();
        OnDataManipulationComplete();
        SaveTextFile();
        return 0;
        }

        private int ReadTextFile()
        {
            //Read the text File
            return 0;

        }

        private int ParsingData()
        {
            // Data manipulation
            return 0;

        }

        private int SaveTextFile()
        {

            // save the file
            return 0;
        }

        public void OnFileReadingComplete()
        {
            EventHandler handler = FileReadingComplete;

            if (handler != null) {
                handler(this, EventArgs.Empty);
            }
        }

        public void OnDataManipulationComplete()
        {
            EventHandler handler = DataManipulationComplete;

            if (handler != null) {
                handler(this, EventArgs.Empty);
            }
        }
    }