我有一个表单,它被后台工作人员称为对话框。在此对话框中,有一个组合框和几个按钮。selectedindexchanged
我有一个基于组合框的带有手柄的子。
在此子的条件之一内,组合框的选定索引被更改。更改时selectedindex
,调试器似乎停止在当前行并selectedindexchanged
再次调用 sub。它通过子程序运行到最后没有错误,然后在更改索引的行之后恢复。但是,当子例程的第一个实例结束时,从后台工作程序调用对话框的行会引发错误。Exception has been thrown by the target of an invocation.
所以在这里我基本上有两个问题,可以用一种解决方案来解决。首先如何停止selecetedindexchanged
sub 运行两次,其次如何在 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