2010-06-23 19 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