2010-06-23 38 views
1

这个问题是关于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; 
      } 
     } 
    } 

是否有可能通过委托和事件....如果你有任何疑问plz回复我

回答

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); 
      } 
     } 
    } 
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(); 
    } 
} 
+0

喜,贾斯汀.....你能告诉我的顺序呼叫button_click事件 – user374191 2010-06-23 12:27:40

+0

使用@ user374191 - 我不知道你的意思是什么。 – 2010-06-23 12:32:12

+0

FileRead(EventArgs.Empty); -----怎么可能......只需要一个参数 – user374191 2010-06-23 12:37:38