带有“selectedindexchanged"句柄的子调用自身 [英] sub with `selectedindexchanged` handle calls itself

查看:26
本文介绍了带有“selectedindexchanged"句柄的子调用自身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form which is called as a dialogbox from a backgroundworker. On this dialog box there is a combo box along with a couple of buttons. I have a sub with the handle selectedindexchanged based on the combo box.

在此子项的条件之一内,组合框的选定索引已更改.当 selectedindex 更改时,调试器似乎停在当前行并再次调用 selectedindexchanged 子.它通过子例程运行到最后,没有错误,然后在更改索引的行之后恢复.但是,当子例程的第一个实例结束时,从后台工作程序调用对话框的行会抛出错误.调用的目标已抛出异常.

Within one of the conditions of this sub the selected index of the combo box is changed. When the selectedindex is changed the debugger appears to stop on the current line and call the selectedindexchanged sub a second time. It runs through the subroutine to the end with no errors and then resumes after the line where the index is changed. However when the first instance of the subroutine ends, an error is thrown by the line that calls the dialogbox from the backgroundworker. Exception has been thrown by the target of an invocation.

所以在这里我基本上有两个问题,可以通过一个解决方案来解决.首先如何阻止selecetedindexchanged sub 运行两次,其次如何在sub 结束时出错.

So here I essentially have two problems, that could be solved with one solution. Firstly how to stop the selecetedindexchanged sub from running twice, and secondly how to the error when the sub ends.

示例代码如下.

调用对话框的代码:

 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 的代码私有子填充站点()

Code for Fillsites Private Sub 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

推荐答案

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

There a several ways to stop the event handler from running twice, two of them being:

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

Easy
You could set a member variable to indicate whether the event handler is currently executing and only execute the event handler if the flag is not set:

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 事件的事件处理程序,并在完成事件处理程序之前重新添加.

Advanced
Remove the event handler for the cmbSites.SelectedIndexChanged event when entering the event handler and re-add it before finishing the event handler.

这篇关于带有“selectedindexchanged"句柄的子调用自身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆