首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >窗体在load事件上不显示控件

窗体在load事件上不显示控件
EN

Stack Overflow用户
提问于 2020-08-26 21:52:26
回答 1查看 71关注 0票数 0

我有一个表单,可以检查文件夹中是否有一些更改。我用FileSystemWatcher来做这件事。当通知更改时,必须加载另一个表单。第二个窗体已加载,但控件不可见。当我通过单击按钮加载第二个窗体时,控件是可见的。我想这是因为If语句,但我不确定。有人能帮我解决这个问题吗?

代码语言:javascript
复制
Imports System.IO
'Imports System.Diagnostics

Public Class Settings
  Public watchfolder As FileSystemWatcher
  Private Sub Settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    txt_watchpath.Text = "D:\temp"
    If txt_watchpath.Text = "" Then
        btn_startwatch.Enabled = False
    End If
  End Sub

  Private Sub txt_watchpath_TextChanged(sender As Object, e As EventArgs) Handles txt_watchpath.TextChanged
    If Me.Text <> "" Then
        btn_startwatch.Enabled = True
    End If
  End Sub

  Private Sub btn_startwatch_Click(sender As Object, e As EventArgs) Handles btn_startwatch.Click
    watchfolder = New System.IO.FileSystemWatcher()

    'this is the path we want to monitor
    watchfolder.Path = txt_watchpath.Text

    'Add a list of Filter we want to specify
    'make sure you use OR for each Filter as we need to
    'all of those 

    watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
    IO.NotifyFilters.LastWrite
    watchfolder.NotifyFilter = watchfolder.NotifyFilter Or
    IO.NotifyFilters.Attributes

    ' add the handler to each event
    AddHandler watchfolder.Changed, AddressOf logchange
    AddHandler watchfolder.Created, AddressOf logchange
    'AddHandler watchfolder.Deleted, AddressOf logchange

    ' add the rename handler as the signature is different
    AddHandler watchfolder.Renamed, AddressOf logrename

    'Set this property to true to start watching
    watchfolder.EnableRaisingEvents = True
    watchfolder.IncludeSubdirectories = False

    btn_startwatch.Enabled = False
    btn_stop.Enabled = True
    Me.Hide()

    'End of code for btn_start_click
  End Sub

  Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

    If e.ChangeType = IO.WatcherChangeTypes.Changed Then
        FrmPopup.Fullpath = e.FullPath
        FrmPopup.Activity = e.Name & " is changed"
        FrmPopup.Show()
    End If

    If e.ChangeType = IO.WatcherChangeTypes.Created Then
        FrmPopup.Fullpath = e.FullPath
        FrmPopup.Activity = e.Name & " is made"
        test.Show()
    End If
   
  End Sub

  Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)

    FrmPopup.Fullpath = e.FullPath
    FrmPopup.Activity = e.OldName & " is changed to " & e.Name
    FrmPopup.Show()

  End Sub

  Private Sub btn_stop_Click(sender As Object, e As EventArgs) Handles btn_stop.Click
    FrmPopup.Show()
  End Sub
End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-27 00:54:56

FileSystemWatcher事件不是由UI线程生成的。要进入你的UI线程,你需要使用类似这样的东西:

代码语言:javascript
复制
Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

    If InvokeRequired Then
        Me.Invoke(New MethodInvoker(Sub()

                                        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
                                            FrmPopup.Fullpath = e.FullPath
                                            FrmPopup.Activity = e.Name & " is changed"
                                            FrmPopup.Show()
                                        End If

                                        If e.ChangeType = IO.WatcherChangeTypes.Created Then
                                            FrmPopup.Fullpath = e.FullPath
                                            FrmPopup.Activity = e.Name & " is made"
                                            test.Show()
                                        End If

                                    End Sub))
    End If

End Sub

Private Sub logrename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)

    If InvokeRequired Then
        Me.Invoke(New MethodInvoker(Sub()

                                        FrmPopup.Fullpath = e.FullPath
                                        FrmPopup.Activity = e.OldName & " is changed to " & e.Name
                                        FrmPopup.Show()

                                    End Sub))
    End If

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63599185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档