FileSystemWatcher Changed Event [resolved]
Hey guys, this is probably a simple question, but I am new to VB .net....
I have a FileSystemWatcher object in a Module and it is declared in that Module as public. The variable name is FileWatch. I also have a FileWatch.Changed() event in the same module, but it gives me an error whenever I try to compile:
error BC30506: Handles clause requires a WithEvents variable.
That error points to:
VB Code:
Public Sub FileWatch_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileWatch.Changed
I know it's because it is in a module, and not in a form's code, but what do I add with WithEvents?
Thanks,
jonwondering
Re: FileSystemWatcher Changed Event
I don't know the answer.
However, the error sorta tells you the error.
If I were to guess, when you declare it, put withevents someone in the declaration.
I don't even know what that means, but I have seen and debugged programs where that was used.
- Josh
Re: FileSystemWatcher Changed Event
To use the FileSystemWatcher tool, you may add it to the form like a timer or ToolTip. Or when you declare the object manually and want to use its event than declare it with the WithEvents keyword.
Dim WithEvents FileWatch As IO.FileSystemWatcher
Re: FileSystemWatcher Changed Event
That solved the compiling problem, but when in the same module I do this:
VB Code:
FileWatch.NotifyFilter = FileWatch.LastAccess
then, application crashes at runtime saying Object reference not set to an instance of an object....
Re: FileSystemWatcher Changed Event
Add the New Keyword in the declaration ...
Dim WithEvents FileWatch As IO.FileSystemWatcher = New IO.FileSystemWatcher
Re: FileSystemWatcher Changed Event
Yeah that works. Thank you very much...
But now for some reason the FileWatch.Changed event is not working at all - it is not executed... could it be because it's located in a module?
Thanks
Re: FileSystemWatcher Changed Event
Have you set the
FileWatch.EnableRaisingEvents = True
It should look like this ....
VB Code:
Private WithEvents FileWatch As IO.FileSystemWatcher
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileWatch = New IO.FileSystemWatcher("C:\Toto")
FileWatch.NotifyFilter = IO.NotifyFilters.FileName
FileWatch.EnableRaisingEvents = True
End Sub
Re: FileSystemWatcher Changed Event
Oh yes. I am sorry - the last one was my stupid mistake. I fixed it now. Thanks for your help Zakary!
Re: FileSystemWatcher Changed Event