0

我有一个表单,它被后台工作人员称为对话框。在此对话框中,有一个组合框和几个按钮。selectedindexchanged我有一个基于组合框的带有手柄的子。

在此子的条件之一内,组合框的选定索引被更改。更改时selectedindex,调试器似乎停止在当前行并selectedindexchanged再次调用 sub。它通过子程序运行到最后没有错误,然后在更改索引的行之后恢复。但是,当子例程的第一个实例结束时,从后台工作程序调用对话框的行会引发错误。Exception has been thrown by the target of an invocation.

所以在这里我基本上有两个问题,可以用一种解决方案来解决。首先如何停止selecetedindexchangedsub 运行两次,其次如何在 sub 结束时出错。

下面的示例代码。

调用对话框的代码:

 If diaImpSelectedSite.IsAccessible = False Then diaImpSelectedSite.Activate()
                    diaImpSelectedSite.RequestSender = Me
                    diaImpSelectedSite.ShowDialog()
                    DialogResult = diaImpSelectedSite.Result()
                    diaImpSelectedSite.Close()

代码SelectedIndexChanged

 Private Sub cmbSites_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbSites.SelectedIndexChanged
    If Not cmbSites.SelectedIndex = -1 Then
        If cmbSites.SelectedIndex = cmbSites.Items.Count - 1 Then
            MsgBox(Str(cmbSites.SelectedIndex) & "/" & Str(cmbSites.Items.Count))
            Dim result As String = funAddSite()
            FillSites()
            cmbSites.SelectedIndex = SiteFromSiteName(result) - 1
            Exit Sub
        Else
            bttYes.Enabled = True
        End If
    End If
End Sub

上述子函数中的代码

Private Function funAddSite()
    Dim showndiaAddsite As diaAddSite = New diaAddSite()
    showndiaAddsite.RequestSender = Me
    showndiaAddsite.ShowDialog()
    Return showndiaAddsite.Result()
    showndiaAddsite.Close()
End Function

Fillsites 私人子 FillSites()的代码

    Dim RecordCounter As Integer
    Dim sitetags As New List(Of Integer)
    cmbSites.Items.Clear()

    If Not UserSites(0) Is Nothing Then
        For RecordCounter = 1 To Sites.Rows - 1 Step 1
            If Not Sites.Value(0, RecordCounter) = Nothing Then
                sitetags.Add(RecordCounter)
                cmbSites.Items.Insert(cmbSites.Items.Count, "TTSite_" & Format(RecordCounter, "000") & " " & Sites.Value(0, RecordCounter))
            End If
        Next
    End If
    cmbSites.Items.Insert(cmbSites.Items.Count, "New Site...")
    cmbSites.Text = "Select Site..."
End Sub
4

1 回答 1

0

有几种方法可以阻止事件处理程序运行两次,其中两种是:

简单
您可以设置一个成员变量来指示事件处理程序当前是否正在执行,并且仅在未设置标志时才执行事件处理程序:

Private Sub cmbSites_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbSites.SelectedIndexChanged
    If Not m_alreadyInSelectedIndexChanged And Not cmbSites.SelectedIndex = -1 Then
        m_alreadyInSelectedIndexChanged = True
        Try
            If cmbSites.SelectedIndex = cmbSites.Items.Count - 1 Then
                MsgBox(Str(cmbSites.SelectedIndex) & "/" & Str(cmbSites.Items.Count))
                Dim result As String = funAddSite()
                FillSites()
                cmbSites.SelectedIndex = SiteFromSiteName(result) - 1
                Exit Sub
            Else
                bttYes.Enabled = True
            End If
        Finally
            m_alreadInSelectedIndexChanged = False
        End Try
    End If
 End

高级
进入事件处理程序时 删除事件cmbSites.SelectedIndexChanged的事件处理程序,并在完成事件处理程序之前重新添加。

于 2013-01-08T11:17:53.860 回答