Home > Software engineering >  Add event to FileSystemWatcher when property EnableRaisingEvents changed
Add event to FileSystemWatcher when property EnableRaisingEvents changed

Time:10-27

I have created a custom class

Public Class MyFSW
    Inherits FileSystemWatcher
    Public Property ParentForm As Form
    Public Property TabPage As TabPage
End Class

Now I want to add a custom event to the this class, that fires when the property "EnableRaisingEvents" of the FileSystemWatcher changes?

Is there any chance to do this?

CodePudding user response:

The EnableRaisingEvents property is not declared Overridable so you can't override it. You can shadow it though:

Public Class FileSystemWatcherEx
    Inherits FileSystemWatcher

    Public Property ParentForm As Form
    Public Property TabPage As TabPage

    Public Shadows Property EnableRaisingEvents As Boolean
        Get
            Return MyBase.EnableRaisingEvents
        End Get
        Set(value As Boolean)
            If MyBase.EnableRaisingEvents <> value Then
                MyBase.EnableRaisingEvents = value
                OnEnableRaisingEventsChanged(EventArgs.Empty)
            End If
        End Set
    End Property

    Public Event EnableRaisingEventsChanged As EventHandler

    Protected Overridable Sub OnEnableRaisingEventsChanged(e As EventArgs)
        RaiseEvent EnableRaisingEventsChanged(Me, e)
    End Sub

End Class

That will work as long as you set the property through a reference of type FileSystemWatcherEx. Because the property is shadowed rather than overridden, setting it through a reference of type FileSystemWatcher will bypass the derived property implementation and the event will not be raised. You really can't do anything about that.

CodePudding user response:

As a continuation of a “previous example” you can achieve that with some small changes like the example below shows:

In this example the property is changed by clicking a button, but you can implement handlers on your own needs.

Option Strict On
Imports System.IO


Public Class Form1
    Private Fsw As CustomFSW


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateFSW()
    End Sub

    Sub CreateFSW()

        Fsw = New CustomFSW With {
                .Name = "Tab1", 'Adding this you can specify the object is sending the event
                .Path = "C:\Users\UserName\Desktop\Test\",
                .Filter = "*.*",
                .IncludeSubdirectories = True,
                .EnableRaisingEvents = True
                }


        AddHandler Fsw.Created, AddressOf Fsw_Event
        AddHandler Fsw.Changed, AddressOf Fsw_Event
        AddHandler Fsw.Renamed, AddressOf Fsw_Event
        AddHandler Fsw.Deleted, AddressOf Fsw_Event

        'Here the handler of your custom event
        AddHandler Fsw.MyCutomEvent, AddressOf Fsw_CutomEvent

    End Sub

    Private Class CustomFSW
        Inherits FileSystemWatcher

        Private counter As Integer = 0
        Public Property Name As String

          'You can use the base proeprty insted of this for specific needs
        Private _EnableRaisingEvents As Boolean
        Public Overloads Property EnableRaisingEvents As Boolean
            Get
                Return _EnableRaisingEvents
            End Get
            Set(value As Boolean)
                If Not value = _EnableRaisingEvents Then
                    counter  = 1
                    RaiseEvent MyCutomEvent(Me, "Ciaooo, EnableRaisingEvents is changed " & counter.ToString & " times")
                End If
                _EnableRaisingEvents = value
            End Set
        End Property

        'Rename this on your needs 
        Public Event MyCutomEvent(sender As Object, e As String)


    End Class


    Private Sub Fsw_CutomEvent(sender As Object, e As String)
        ' Do your stuf here 
        MsgBox(e)
    End Sub


    Private Sub Fsw_Event(sender As Object, e As FileSystemEventArgs)
        Dim FSW As CustomFSW = CType(sender, CustomFSW)
        If Not String.IsNullOrEmpty(FSW.Name) Then
            Select Case FSW.Name
                Case "Tab1"
                    'Do something
                    Debug.WriteLine("Event genarated from: " & FSW.Name)

                Case "Tab2"
                    'Do something else 
                    Debug.WriteLine("Event genarated from: " & FSW.Name)

            End Select
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Fsw IsNot Nothing Then
            Fsw.EnableRaisingEvents = Not Fsw.EnableRaisingEvents
        End If
    End Sub



End Class

CodePudding user response:

Thank you so far! This is what I got now (in my class-file):

Public Class MyFSW
    Inherits FileSystemWatcher
    Public Property ParentForm As Form
    Public Property TabPage As TabPage

    Public Shadows Property EnableRaisingEvents As Boolean
        Get
            Return MyBase.EnableRaisingEvents
        End Get
        Set(value As Boolean)
            If MyBase.EnableRaisingEvents <> value Then
                MyBase.EnableRaisingEvents = value
                OnEnableRaisingEventsChanged(EventArgs.Empty)
            End If
        End Set
    End Property

    Public Event EnableRaisingEventsChanged As EventHandler

    Protected Overridable Sub OnEnableRaisingEventsChanged(e As EventArgs)
        RaiseEvent EnableRaisingEventsChanged(Me, e)
    End Sub
End Class

In my Form-Class is got this:

Sub CreateFSW()
    w.Watcher = New MyFSW With {
            .ParentForm = f,
            .TabPage = tp,
            .Path = w.Path,
            .Filter = w.Filter,
            .IncludeSubdirectories = w.IncludeSubdirs,
            .NotifyFilter = DirectCast(w.NotifyFilter, NotifyFilters)
            }

    AddHandler w.Watcher.Created, AddressOf Fsw_EventRaise
    AddHandler w.Watcher.Changed, AddressOf Fsw_EventRaise
    AddHandler w.Watcher.Renamed, AddressOf Fsw_EventRaise
    AddHandler w.Watcher.Deleted, AddressOf Fsw_EventRaise

    AddHandler w.Watcher.EnableRaisingEventsChanged, AddressOf Fsw_Event
End Sub

Private Sub Fsw_Event(sender As Object, e As FileSystemEventArgs)
    MessageBox.Show("Works")
End Sub

The compiler says:

Option Strict On does not allow narrowing in implicit type conversions between method Fsw_Event(sender As Object, e As FileSystemEventArgs) and delegate Delegate Sub EventHandler(sender As Object, e As EventArgs)

Seems that some types does not match. I tried to change FileSystemEventArgs to EventArgs and stuff like this, but without luck.

  • Related