Results 1 to 9 of 9

Thread: FileSystemWatcher Changed Event [resolved]

  1. #1

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Resolved 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:
    1. 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.

  2. #2
    Member
    Join Date
    Jun 2005
    Posts
    48

    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

  3. #3
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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
    Using VS 2010 on Fw4.0

  4. #4

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: FileSystemWatcher Changed Event

    That solved the compiling problem, but when in the same module I do this:
    VB Code:
    1. FileWatch.NotifyFilter = FileWatch.LastAccess
    then, application crashes at runtime saying Object reference not set to an instance of an object....

  5. #5
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: FileSystemWatcher Changed Event

    Add the New Keyword in the declaration ...

    Dim WithEvents FileWatch As IO.FileSystemWatcher = New IO.FileSystemWatcher
    Using VS 2010 on Fw4.0

  6. #6

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    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

  7. #7
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: FileSystemWatcher Changed Event

    Have you set the

    FileWatch.EnableRaisingEvents = True

    It should look like this ....

    VB Code:
    1. Private WithEvents FileWatch As IO.FileSystemWatcher
    2.  
    3. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     FileWatch = New IO.FileSystemWatcher("C:\Toto")
    5.     FileWatch.NotifyFilter = IO.NotifyFilters.FileName
    6.     FileWatch.EnableRaisingEvents = True
    7.  
    8. End Sub
    Using VS 2010 on Fw4.0

  8. #8

    Thread Starter
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    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!

  9. #9
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: FileSystemWatcher Changed Event

    Your are welcome
    Using VS 2010 on Fw4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width