Home > Software engineering >  Identify parent of FileSystemWatcher
Identify parent of FileSystemWatcher

Time:10-27

I have a TabControl in my main form and in every TabPage is a form dynamicially added. These (sub)forms contain severeal controls (textboxes, checkboxes, etc.) and also a FileSystemWatcher.

When adding the form in the TabPage, the FileSystemWatcher is initialized with path, filter etc. and EnableRaisingEvents are set to true.

Here the code for this part (from my main form):

Private Sub ApplyConfig
    For Each w As ClsCfgWatcher In MyWatchers
       '// create TabPage
       Call CreateTabPageWatcher(w)
    Next
End Sub

Private Sub CreateTabPageWatcher(w As ClsCfgWatcher)
    '// add tabpage
    TabWatcher.TabPages.Add(w.Name)
    Dim tp As TabPage = TabWatcher.TabPages(w.Name)
    With tp
        .Text = w.Name
        .UseVisualStyleBackColor = True
    End With

    '// adding form
    Dim f As New FrmCfgWatcher With {
        .TopLevel = False,
        .Anchor = AnchorStyles.Right Or AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Bottom,
        .Width = TabWatcher.SelectedTab.Width,
        .Height = TabWatcher.SelectedTab.Height
    }
    tp.Controls.Add(f)
    f.Show()

    '// write values to form from config
    With f
        .TxtName.Text = w.Name
        .TxtPath.Text = w.Path
        .CbxSubDirs.Checked = w.IncludeSubdirs
        .TxtFilter.Text = w.Filter

        .TxtCommand.Text = w.ActionCommand
        .TxtParameter.Text = w.ActionParameter
        .TxtWorkingDir.Text = w.ActionWorkingDir

        '// create FilesystemWatcher
        .Fsw = New FileSystemWatcher With {
                .Path = w.Path,
                .Filter = w.Filter,
                .IncludeSubdirectories = w.IncludeSubdirs,
                .NotifyFilter = DirectCast(w.NotifyFilter, NotifyFilters)
                }

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

        w.Watcher.EnableRaisingEvents = True
    End With
End Sub

The events do raise as expected but now I need to know which FileSystemWatcher raised exactly? For example from which TabPage, because I need some Information from that Tabpage in my sub that handles the event.

Some more code (from form in tabpage):

Private Sub Fsw_Event(sender As Object, e As FileSystemEventArgs)
If e.FullPath > "" Then
    Dim sCmd As String = [text from textbox on form]
End If
End Sub

I need some Link between the FileSystemWatcher-Object an my TabPage or form nd don't know how to realize that.

Can anyone help?

CodePudding user response:

There are several techniques to do that. Depends on self-needs which one of those someone could use. In your case I think a way might be that you can create a Custom class inheriting as a base class FileSystemWatcher.

At this point you can create your own property called Name valuing that with the Tab name which contain the FileSystemWatcher. In your Event handler you can intercept that Name then executing code based on the name of object that have raised the event.

Here an Example:

Option Strict On
Imports System.IO


Public Class Form1


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

    Sub CreateFSW()

        Dim 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


    End Sub

    Private Class CustomFSW
        Inherits FileSystemWatcher
        Public Property Name As String
    End Class


    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 generated from: " & FSW.Name)

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

            End Select
        End If

    End Sub


End Class
  • Related