RaiseEvent in csharp

A

awk

You havent declared an event of type one of your delegates in your code.

eg
public event Changed FileWatcherChanged;

and you raise this

protected virtual void OnChanged(object sender, FileSystemEventArgs e)
{
changed(e.ChangeType);
}

Regards,

A
 
N

Nicolas

How do I do a RaiseEvent in csharp
I'm ok in VB but csharp confused me a lot.

******* code ********
private FileSystemWatcher watcher = new FileSystemWatcher();

public delegate void Changed(WatcherChangeTypes exch);
public delegate void Created(string fileName);
public delegate void ReCreated(string OldName, string newName);
public delegate void OverRun(string Message);

public FileWatcher()
{
watcher.Path = "C:\\";

//* Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;

// Only watch text files.
watcher.Filter = "*.txt";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.Error += new ErrorEventHandler(OnError);

// Begin watching.
watcher.EnableRaisingEvents = true;

}

protected virtual void OnChanged(object sender, FileSystemEventArgs e)
{
///////////////////////////////////////// THIS IS WHERE IT DOESN'T WORK
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//Changed(e.ChangeType);
}

protected virtual void OnCreated(object sender, FileSystemEventArgs e)
{
///////////////////////////////////////// THIS IS WHERE IT DOESN'T WORK
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//RaiseEvent Created(e.FullPath);
}

protected virtual void OnRenamed(object sender , RenamedEventArgs a)
{
///////////////////////////////////////// THIS IS WHERE IT DOESN'T WORK
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//RaiseEvent ReCreated(e.OldFullPath, e.FullPath);
}



Thank you
 
N

Nicolas

Thanks, but now how do I call those functions from another form as all this
is in a dll now.

This is what I had in VB
////////////////// CODE \\\\\\\\\\\\\\\\\\\\
Imports it

Dim fw As New it_FileWatcher.FileWatcher()

AddHandler fw.Changed, AddressOf onChange

Sub onChange(ByVal changeType As System.IO.WatcherChangeTypes, ByVal
filename As String)

System.Diagnostics.Process.Start(filename)

End Sub

what should I do to get the same in csharp???



Thank you very much for the help.
 
A

awk

Hi Nicolas

I think this is what your after.

it_FileWatcher.FileWatcher fw = new it_FileWatcher.FileWatcher();
fw.Changed += new it_FileWatcher.Changed(onChange);

private void onChange(System.IO.WatcherChangeTypes change, string filename)
{
System.Diagnostics.Process.Start(filename);
}

For more info check these out.

this link for some detail delegate and event coding in c#
with vb equivelants for comparison
http://codeproject.com/dotnet/observerlistview.asp

and

http://www.thecodeproject.com/csharp/delegate_bedtime.asp

Simon
 
N

Nicolas

Thank a lot , work ok now!!!
awk said:
Hi Nicolas

I think this is what your after.

it_FileWatcher.FileWatcher fw = new it_FileWatcher.FileWatcher();
fw.Changed += new it_FileWatcher.Changed(onChange);

private void onChange(System.IO.WatcherChangeTypes change, string filename)
{
System.Diagnostics.Process.Start(filename);
}

For more info check these out.

this link for some detail delegate and event coding in c#
with vb equivelants for comparison
http://codeproject.com/dotnet/observerlistview.asp

and

http://www.thecodeproject.com/csharp/delegate_bedtime.asp

Simon

FileSystemEventArgs
 

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

Similar Threads


Top