嘿所有我正在使用 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)。
任何帮助都可以解决我的这个问题!谢谢!
答案 0 :(得分:3)
不确定这是否适合您,因为我不知道您使用Shared EventHandler的原因,但是如果您将此设置为您的表单,则可以尝试使用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