Custom Event Handler

A

Aaron

I am getting an error down below, this same code works in VB, what gives?

private void Form1_Load(object sender, System.EventArgs e)

{




System.Windows.Forms.LinkLabel l = new System.Windows.Forms.LinkLabel();

FileSystemWatcher watcher = new FileSystemWatcher();

watcher.Path = @"C:\TempFolder";

watcher.Filter = "*.txt";

//error here: Cannot implicitly convert type System.EventHandler to
FileSystemWatcher,

watcher.Created += new System.EventHandler(OnCreated);

watcher.EnableRaisingEvents = true;





}

private void OnCreated(object sender, System.EventArgs e)

{

MessageBox.Show("yo");











}
 
S

Steve Willcock

C# is not as forgiving when it comes to types that don't match - try
compiling the VB code with Option Strict turned on - you'll probably get the
same error.

Replace the references to System.EventHandler with
System.IO.FileSystemEventHandler and this should work

(e.g.)

watcher.Created += new System.IO.FileSystemEventHandler(OnCreated);

private void OnCreated(object sender, System.IO.FileSystemEventArgs e)
 
E

Edd Connolly

Aaron,

The FileSystemWatcher's Created event is of type
System.IO.FileSystemEventHandler. You cannot substitute it for
System.EventHandler.

Change it to the following...

watcher.Created = new System.IO.FileSystemEventHandler(OnCreated);

And method should have the signature...

private void OnCreated (object sender, System.IO.FileSystemEventArgs e)
{
...
}
 
E

Edd Connolly

Oops, Make that...

watcher.Created += new System.IO.FileSystemEventHandler(OnCreated);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top