Company      |       Articles & Research      |      Services      |      Software      |      Contact

EventArgs Example

In one my past posts, I talked about that CryptoCollaboration using the EventArgs base class because it keeps things nice and generalized, allowing for expanasion for the handling of future event integration. A person emailed me and asked for a practical example of when would use such a thing in a SharePoint WebPart. Eh, ok.

I am going to use an example of a progress class, that, well, as you can guess will display the progress of some sort of execution that happens with a SharePoint WebPart.

The first class is the Process Arguments class (ProcessArgs), that inherits from the EventArgs base class.

C#:
  1. public class ProgressArgs : EventArgs
  2. {
  3. private int m_nPercentageComplete = 0;
  4. private DataRow m_oDataRow;
  5.  
  6. public ProgressArgs(DataRow dataRow, int percentageComplete)
  7. {
  8. this.m_oDataRow = dataRow;
  9. this.m_nPercentageComplete = percentageComplete;
  10. }
  11.  
  12. public DataRow CurrentDataRow
  13. {
  14. get
  15. {
  16. return this.m_oDataRow;
  17. }
  18. }
  19.  
  20. public int PercentageComplete
  21. {
  22. get
  23. {
  24. return this.m_nPercentageComplete;
  25. }
  26. }
  27. }

In the next class, there will be the establishment of the event handler that will be called from a sister class by use of a delegate.

C#:
  1. public delegate void ProgressEventHandler(object sender, ProgressArgs e);

Now that the delegate is established, you can call it in some general class file.

C#:
  1. public event ProgressEventHandler ProgressEvent;
  2.  
  3. protected virtual void OnProgress(ProgressArgs e)
  4. {
  5. if (this.ProgressEvent != null)
  6. {
  7. this.ProgressEvent(this, e);
  8. }
  9. }

Then calculate out the progress.

C#:
  1. ProgressArgs e = new ProgressArgs(row, (int) ((((double) num) / ((double) this.DBDataTable.Rows.Count)) * 100));
  2. this.OnProgress(e);

I am not saying that this is the only way to do it, but with applications that you plan on extending later it is the route that I have choosen to go in the past. I may at some point explore the deficiencies in a such an approach, however at this current time I just want to finish the damn thing.

share save 171 16 EventArgs Example

Related posts:

  1. Event Handling In InfoPath
  2. CryptoCollaboration Client Testing / Encryption Optimization
  3. Using the Wizard Control In WebParts
  4. Open Configuration Panel From WebPart ToolPane
  5. Protected Constructors For Abstract Classes

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment