Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9093285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:03:35+00:00 2026-06-16T23:03:35+00:00

I have a form which is called as a dialogbox from a backgroundworker. On

  • 0

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.

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.

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.

Sample code below.

Code to call the Dialog box:

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

Code for 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

Code for the function in the above sub

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

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:03:36+00:00Added an answer on June 16, 2026 at 11:03 pm

    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
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in which the radio buttons and checkboxes look like this:
I have a form on which there is a text field called Role. Onload
I have this PHP contact form which disallows a submission from an existing IP.
I have a base and derived class called Node which form elements of a
I have a windows form app, and a class which is called Game. Let's
I have a view which renders a partial view and in this view there
I have a simple form which accepts a Title and a Contents variable from
I a have a django form which is a select field called piece which
I have a class library which is called through COM from a Third Party
I have a Windows form which hosts a custom User Control. This User Control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.