0

我编写了一个 WinForm 项目,该项目显示一个ListBox包含文件名列表。当用户单击提交按钮时,应用程序会动态加载并PictureBox为每个文件显示一个控件,然后在处理它们时等待。由于为每个文件生成 PDF 文件,因此PictureBox需要更新该文件的匹配项以显示图像。

这是我到目前为止所拥有的:

Private Sub ButtonSubmit_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSubmit.Click

    Dim x As Integer = 790
    Dim y As Integer = 91
    For i As Integer = 0 To ListBox1.Items.Count - 1

        Dim key As String = ListBox1.Items(i).ToString()

        'adds picturebox for as many listbox items added
        Dim MyPictureBox As New PictureBox()
        MyPictureBox.Name = "pic" + key
        MyPictureBox.Location = New Point(x, y)
        MyPictureBox.Size = New Size(12, 12)
        MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        Me.Controls.Add(MyPictureBox)
        MyPictureBox.Image = My.Resources.Warning1
        ToolTipSpooling.SetToolTip(MyPictureBox, "Creating PDF...")
        x += 0
        y += 13

    Next i

    Call CheckPDFs()

End Sub

Public Sub CheckPDFs()
    Dim ListboxTicketIDs = (From i In ListBox1.Items).ToArray()

    For Each Item In ListboxTicketIDs
        Dim ID = Item.ToString

        Dim Watcher As New FileSystemWatcher()
        Watcher.Path = "C:\Temp\"
        Watcher.NotifyFilter = (NotifyFilters.Attributes)
        Watcher.Filter = ID + ".pdf"

        AddHandler Watcher.Changed, AddressOf OnChanged

        Watcher.EnableRaisingEvents = True
    Next

End Sub

Private Sub OnChanged(source As Object, e As FileSystemEventArgs)

    Dim p As PictureBox = CType(Me.Controls("pic" + ListBox1.Items.ToString()), PictureBox)
    p.Image = My.Resources.Ok1

End Sub

一旦列表框中列出的项目存在,我无法将 更改PictureBox为不同的图片,基于FileSystemWatcher. 例如,文件并不总是按照它们在ListBox.

编辑

下面的工作代码。

Public Class Form1

Private WithEvents Watcher As FileSystemWatcher

Public Sub CheckPDFs()
    For i As Integer = 0 To ListBox1.Items.Count - 1
        Watcher = New FileSystemWatcher()
        Watcher.SynchronizingObject = Me
        Watcher.Path = "C:\Temp\"
        Watcher.NotifyFilter = NotifyFilters.Attributes
        Watcher.Filter = "*.pdf"

        Watcher.EnableRaisingEvents = True
    Next
End Sub

Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed

    Dim key As String = Path.GetFileNameWithoutExtension(e.Name)
    Dim p As PictureBox = CType(Me.Controls("pic" + key), PictureBox)
    p.Image = My.Resources.Ok

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ListBox1.Items.Add(TextBox1.Text)
    TextBox1.Text = ""

    Dim x As Integer = 5
    Dim y As Integer = 5
    For i As Integer = 0 To ListBox1.Items.Count - 1

        Dim key As String = ListBox1.Items(i).ToString()

        'adds picturebox for as many listbox items added
        Dim MyPictureBox As New PictureBox()
        MyPictureBox.Name = "pic" + key
        MyPictureBox.Location = New Point(x, y)
        MyPictureBox.Size = New Size(15, 15)
        MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
        Me.Controls.Add(MyPictureBox)
        MyPictureBox.Image = My.Resources.Info
        x += 0
        y += 18

    Next i

    Call CheckPDFs()
End Sub
4

1 回答 1

1

首先,您不需要创建多个文件观察程序。您只需要一个文件观察器来观察文件夹的任何更改。我建议使用关键字在表单顶部将其声明为私有字段,WithEvents这样您就不必担心添加和删除事件处理程序。

接下来,当观察者引发更改事件时,您可以通过查看事件 args 对象的属性来获取更改文件的文件名。您需要获取更改的文件的名称,然后使用文件名作为查找匹配图片框控件的关键。

Public Class Form1
    Private WithEvents Watcher As FileSystemWatcher

    Public Sub CheckPDFs()
        Watcher = New FileSystemWatcher()
        Watcher.Path = "C:\Temp\"
        Watcher.NotifyFilter = NotifyFilters.Attributes
        Watcher.Filter = "*.pdf"
    End Sub

    Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed
        Dim key As String = Path.GetFileNameWithoutExtension(e.Name)
        Dim p As PictureBox = CType(Me.Controls("pic" + key), PictureBox)
        p.Image = My.Resources.Ok1
    End Sub
End Class

但是,由于您在下面的评论中说文件名与列表框中的文本不同,而是仅以该文本开头,因此您可以执行以下操作:

Private Sub Watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs) Handles Watcher.Changed
    Dim p As PictureBox = Nothing
    For Each item As Object In ListBox1.Items
        If e.Name.StartsWith(item.ToString()) Then
            p = CType(Me.Controls("pic" + item.ToString()), PictureBox)
            Exit For
        End If
    Next
    If p IsNot Nothing Then
        p.Image = My.Resources.Ok1
    End If
End Sub
于 2012-09-17T21:00:53.040 回答