【问题标题】:VB.NET Delegate Function returning Boolean valueVB.NET 委托函数返回布尔值
【发布时间】:2014-11-02 15:20:56
【问题描述】:

我正在使用 BackgroundWorker,我需要使用委托函数来查看是否检查了 ListViewItem,但我不断收到跨线程错误。这一定是我写它的方式。有什么帮助吗?

    Dim delListViewItemChecked As ListViewItemCheckedDelegate = AddressOf ListViewItemChecked
    delListViewItemChecked.Invoke(ListViewPhotos, 0)

Private Delegate Function ListViewItemCheckedDelegate(ByVal listView As ListView, ByVal index As Integer) As Boolean

Private Function ListViewItemChecked(ByVal listView As ListView, ByVal index As Integer) As Boolean
    If listView.Items(index).Checked = True Then
        Return True
    Else
        Return False
    End If
End Function

【问题讨论】:

标签: vb.net delegates


【解决方案1】:

试试这个:

  1. 不要将 listView 作为参数传递给 ListViewItemCheckedDelegate。
  2. 在后台工作人员的 DoWork 处理程序中声明一个新的委托实例。

此示例似乎可以正常工作:

Private Delegate Function ListViewItemCheckedDelegate(ByVal index As Integer) As Boolean

Private Function ListViewItemChecked(ByVal index As Integer) As Boolean
Return ListView1.Visible
End Function

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

Private Sub bkg1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bkg1.DoWork
Dim delListViewItemChecked As New ListViewItemCheckedDelegate(AddressOf ListViewItemChecked)
MsgBox(Me.Invoke(delListViewItemChecked, 3)) ' arbitrary 3
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2013-03-11
    • 1970-01-01
    • 2017-06-07
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多