【问题标题】:FileSystemWatcher updating textbox on formFileSystemWatcher 更新表单上的文本框
【发布时间】:2014-02-02 05:33:43
【问题描述】:

大家好,我正在使用 FileSystemWatcher 检查 .ini 文件是否已更改。如果有,那么我希望我能够更新表单上的文本框。问题是它是一个共享函数,以便 FileSystemWatcher 工作,所以表单上的任何内容似乎都无法在该共享函数内访问?

我的代码:

Private Sub frmCamera_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim watcher As New FileSystemWatcher()

    watcher.Path = Application.StartupPath
    watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
    watcher.Filter = "*.ini"
    watcher.SynchronizingObject = Me

    AddHandler watcher.Changed, AddressOf OnChanged
    watcher.EnableRaisingEvents = True
End Sub

Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
    Dim sb1 As New StringBuilder(500)
    Dim theButtonsName As String = ""

    GetPrivateProfileString("camera", "val1", "", sb1, sb1.Capacity, Application.StartupPath & "\savedData.ini")

    main.GetPrivateProfileString("camera", "val1", "", sb1, sb1.Capacity, Application.StartupPath & "\savedData.ini")

    Dim frm As frmCamera = DirectCast(DirectCast(source, FileSystemWatcher).SynchronizingObject, frmCamera)
    frm.TextBox1.Text = "debug: " & sb1.ToString
End Sub

我通过主表单调用上面的表单,如下所示:

Public Sub doStuff(ByVal what2Do As String)
   If Trim(lanSent(0)) = "camera" And Trim(lanSent(1)) = "show" Then
      theCam = New Threading.Thread(AddressOf ShowCam)

      theCam.SetApartmentState(Threading.ApartmentState.STA)
      theCam.Start()
   Else
     .....
   End If
End Sub

Private Shared Sub ShowCam()
    Dim ShowCam As New frmCamera
    ShowCam.ShowDialog()
End Sub

我想要完成的是,一旦它看到文本框在其中输入(TextBox1_TextChanged),就运行一些其他的东西。

任何帮助都会很好地解决我的这个问题!谢谢!

【问题讨论】:

    标签: vb.net onchange shared filesystemwatcher


    【解决方案1】:

    不确定这是否对您有用,因为我不知道您为什么使用共享事件处理程序,但是您可以尝试使用FileSystemWatcher.SynchronizingObject 属性,如果您将其设置为您的表单,您可以在您的共享方法中检索它。

    Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
        Dim sb1 As New StringBuilder(500)
        Dim theButtonsName As String = ""
        GetPrivateProfileString("camera", "val1", "", sb1, sb1.Capacity, Application.StartupPath & "\savedData.ini")
    
        Dim frm As Form1 = DirectCast(DirectCast(source, FileSystemWatcher).SynchronizingObject, Form1)
        frm.TextBox1.Text = sb1.ToString
     End Sub
    

    FileSystemWatcher 的初始化

     Dim watcher As New FileSystemWatcher
     watcher.Path = Application.StartupPath
     watcher.SynchronizingObject = Me  'Assigning your Forms Instance to the SychronizingObject
     watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
     watcher.Filter = "*.ini"
     AddHandler watcher.Changed, AddressOf OnChanged
     watcher.EnableRaisingEvents = True
    

    【讨论】:

    • 即使我的代码在同一个表单上,这是否有效?似乎您的代码正在创建另一个表单实例,然后在该表单上填充文本框?
    • 是的。不,它不是创建表单的另一个实例,它只是创建一个变量并将 synchronizingObject 转换为您的表单。它是同一个实例。只需确保将表单分配给 FileSystemWatcher 的 SynchronizingObject 属性。我只是使用 SynchronizingObject 作为存储对表单的引用的地方
    • 'sender' 未声明。由于其保护级别,它可能无法访问。
    • 你称它为源,只需将其更改为那个。我刚刚修改了 OnChanged EventHandler 的签名以匹配您的签名。还将 Form1 更改为您的表单类的名称
    • 检查我上面的更新答案,代码向您展示我如何首先调用表单以及我如何使用您的代码。
    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多