Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find and update a matching control once a file exists
    primarykey
    data
    text
    <p>I have written a WinForm project which displays a <code>ListBox</code> containing a list of file names. When the user clicks a submit button, the application dynamically loads and displays one <code>PictureBox</code> control for each file and then waits while they are processed. As PDF files are generated for each one, the matching <code>PictureBox</code> for that file needs to be updated to display an image. </p> <p>Here's what I have so far: </p> <pre><code>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 </code></pre> <p>I'm having trouble changing the <code>PictureBox</code> to a different picture once the item(s) listed in the listbox are present, based on the <code>FileSystemWatcher</code>. For instance, the files are not always created in the same order as they exist in the <code>ListBox</code>.</p> <p><strong>EDIT</strong></p> <p>Working code below.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload