Click here to Skip to main content
15,891,248 members
Articles / Desktop Programming / Windows Forms

C# Event Implementation Fundamentals, Best Practices and Conventions

Rate me:
Please Sign up or sign in to vote.
4.90/5 (284 votes)
26 Oct 2007CPOL71 min read 773.9K   6.5K   930  
This article presents event implementation fundamentals, best practices, and conventions.
using System;
using System.Windows.Forms;
using System.IO; // used only for the FileSystemWatcher example

namespace CpEventSamples20
{
   public partial class Form1 : Form
   {
      /**********************************************
       * Declarations
       **********************************************/
      FileMover fileMover = new FileMover();
      FileSystemWatcher fsWatcher = new FileSystemWatcher();

      static string fsWatcherTestPath = @"C:\FSWatcherTest";
      string setupNotice = @"The FileSystemWatcher example requires a folder named C:\FSWatcher to exist, and for at least one file to be placed in that folder.";

      /**********************************************
       * Constructor
       **********************************************/
      public Form1()
      {
         InitializeComponent();

         // Wire up event handling methods
         fileMover.MoveFile += new FileMover.MoveFileEventHandler(fileMover_MoveFile);
         fsWatcher.Renamed += new RenamedEventHandler(fsWatcher_Renamed);
      }

      /**********************************************
       * Process end-user gestures
       **********************************************/
      // User Initiates File Move
      private void MoveFileButton_Click(object sender, EventArgs e)
      {
         fileMover.UserInitiatesFileMove();
      }

      // User Initiates File Rename
      private void RenameFileButton_Click(object sender, EventArgs e)
      {
         if (Directory.Exists(fsWatcherTestPath))
         {
            fsWatcher.Path = fsWatcherTestPath;
            fsWatcher.EnableRaisingEvents = true;
            fsWatcher.SynchronizingObject = this;
            string[] files = Directory.GetFiles(fsWatcherTestPath);
            if (files.Length > 0)
            {
               File.Move(files[0], Path.Combine(fsWatcherTestPath, Path.GetFileNameWithoutExtension(files[0]) + "_Renamed" + Path.GetExtension(files[0])));
            }
            else
            {
               MessageBox.Show("The FileSystemWatcher example found no files in the folder: " + fsWatcherTestPath + Environment.NewLine + setupNotice);
            }
         }
         else
         {
            MessageBox.Show("The FileSystemWatcher example did not find the folder: " + fsWatcherTestPath + Environment.NewLine + setupNotice);
         }
         
      }

      /**********************************************
       * Event Handling Methods
       **********************************************/
      void fileMover_MoveFile(object sender, MoveFileEventArgs e)
      {
         this.Results.Text = sender.ToString() + " moved the file, " + e.FileName + ", from " + e.SourceFolder + " to " + e.DestinationFolder;
      }

      void fsWatcher_Renamed(object sender, RenamedEventArgs e)
      {
         this.Results.Text = "The file, " + e.OldName + ", was renamed to " + e.Name;
      }

   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I have been working as an independent consultant since 1994, designing and building business applications mostly in the banking and medical industries.

Comments and Discussions