|
-
Jun 16th, 2005, 11:50 AM
#1
Thread Starter
Hyperactive Member
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
Last edited by jonwondering; Jun 16th, 2005 at 03:00 PM.
-
Jun 16th, 2005, 11:57 AM
#2
Member
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
-
Jun 16th, 2005, 12:15 PM
#3
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
-
Jun 16th, 2005, 02:10 PM
#4
Thread Starter
Hyperactive Member
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....
-
Jun 16th, 2005, 02:22 PM
#5
Re: FileSystemWatcher Changed Event
Add the New Keyword in the declaration ...
Dim WithEvents FileWatch As IO.FileSystemWatcher = New IO.FileSystemWatcher
-
Jun 16th, 2005, 02:31 PM
#6
Thread Starter
Hyperactive Member
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
-
Jun 16th, 2005, 02:38 PM
#7
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
-
Jun 16th, 2005, 02:40 PM
#8
Thread Starter
Hyperactive Member
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!
-
Jun 16th, 2005, 02:42 PM
#9
Re: FileSystemWatcher Changed Event
Your are welcome
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|