0

所以我有一个带有 SelectedIndexChanged 事件处理程序的组合框:

    Private Sub cmbStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbStatus.SelectedIndexChanged
        DoAnnoyingStuff()
        '....
    End sub

在我的表单的其他地方,我有一个执行此操作的函数(在一些用户交互之后):

Sub RefreshStatus()
    Dim sel As Int32 = GetNewStatus()
    cmbStatus.SelectedIndex = sel   '<-- fires a SelectedIndexChanged event that I don't need
End Sub

我想要在 RefreshStatus() 中做的是更改组合框中显示的选择,但不执行 DoAnnoyingStuff()。我该怎么做呢?

4

2 回答 2

1

添加一个标志。因此,您将有一个名为 _inChange As Boolean 的私有类字段,然后在 RefreshStatus 开始时将该字段设置为 true,最后设置为 false。在 cmbStatus_SelectedIndexChanged 中,您检查 _inChange 是否为真。如果是你就退出。如果为假,你继续做 AnnoyingStuff。

于 2013-02-22T11:01:43.243 回答
1

您可以手动管理事件,启用和禁用处理程序:

AddHandler cmbStatus.SelectedIndexChanged, AddressOf cmbStatus_SelIndexChg
RemoveHandler cmbStatus.SelectedIndexChanged, AddressOf cmbStatus_SelIndexChg    

如前所述,您可以使用标志:

Dim bRefreshStatus As Boolean = False

Private Sub cmbStatus_SelIndexChg(sender As Object, e As EventArgs) Handles cmbStatus.SelectedIndexChanged
    If Not bRefreshStatus Then DoAnnoyingStuff()
End Sub

Sub RefreshStatus()
    Dim sel As Int32 = GetNewStatus()
    bRefreshStatus = True
    cmbStatus.SelectedIndex = sel
    bRefreshStatus = False
End Sub
于 2013-02-22T11:14:17.667 回答