user16973
user16973

Reputation: 113

vb.net delegates don't seem to be working

In VB.net, I am trying to use delegates to update a control on a form from a thread, however it doesn't change anything on the form. I've confirmed that it is,in fact, receiving data(so it's not blank), but for some reason it won't send that data to the control.

Delegate Sub fupdatedelegate(ByVal itemtoadd As String)
Dim myupdate As fupdatedelegate = AddressOf updatefrmlist

Private Sub updatefrmlist(ByVal itemtoadd As String) 
    With Form1.ListBox1.Items
        .Add(itemtoadd)
    End With
    MsgBox(itemtoadd)
End Sub

and called as

If Form1.ListBox1.InvokeRequired Then
   Form1.ListBox1.BeginInvoke(myupdate)
End If

how can I make it so that it actually adds the items to the ListBox? (this is being run from a module)

Upvotes: 0

Views: 101

Answers (1)

dbasnett
dbasnett

Reputation: 11773

Try modifying the sub to deal with the invoke. Then just call it with the item to add.

Private Sub updatefrmlist(ByVal itemtoadd As String)
    If Me.InvokeRequired Then
        Me.BeginInvoke(myupdate, itemtoadd)
    Else
        With ListBox1.Items
            .Add(itemtoadd)
        End With
        ' MsgBox(itemtoadd)
    End If
End Sub

All calls will look like

 updatefrmlist(somestring)

Upvotes: 1

Related Questions