none
For loop executing twice-VB.net RRS feed

  • Question

  •    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            For j As Integer = 0 To countx - 1
                UItab1 = System.Configuration.ConfigurationSettings.AppSettings(j)
                Dim tabpage As New TabPage
                tabpage.Text = "Page"
                'tabpage.Controls.Add(tabpage)
                TabControl1.TabPages.Add(tabpage)
                Dim webBrowser As New WebBrowser
                TabControl1.SelectedTab = tabpage
                tabpage.Controls.Add(webBrowser)
                webBrowser.Dock = DockStyle.Fill
                webBrowser.Navigate(UItab1)
            Next
            Exit Sub

    Kindly help me... why the above code is executing twice as the correct count is only 3 and its showng 6 tabs


    • Edited by meeraluv Sunday, August 2, 2020 9:28 AM
    Sunday, August 2, 2020 9:27 AM

Answers

  • Hello,

    Performing those operations first off do not belong in DocumentCompleted event.  For an experiment, place a button on the form, place the code above into the click event, comment out the code in DocumentCompleted. What happens when clicking the button? Do you got three tabs, not six? Let's say you get three tabs and this should happen when the form is shown, place the code in the form shown event, not the load event. 

    On a side note, single letter variables such as j (j name is meaningless) are only used for sample code, recommend to never use single letter variable. I would suggest replacing j say with index.



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 2, 2020 10:40 AM
    Moderator
  • This worked...Thanks a ton.. God bless
    • Marked as answer by meeraluv Tuesday, August 4, 2020 12:13 PM
    Tuesday, August 4, 2020 5:38 AM

All replies

  • Hello,

    Performing those operations first off do not belong in DocumentCompleted event.  For an experiment, place a button on the form, place the code above into the click event, comment out the code in DocumentCompleted. What happens when clicking the button? Do you got three tabs, not six? Let's say you get three tabs and this should happen when the form is shown, place the code in the form shown event, not the load event. 

    On a side note, single letter variables such as j (j name is meaningless) are only used for sample code, recommend to never use single letter variable. I would suggest replacing j say with index.



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, August 2, 2020 10:40 AM
    Moderator
  • This worked...Thanks a ton.. God bless
    • Marked as answer by meeraluv Tuesday, August 4, 2020 12:13 PM
    Tuesday, August 4, 2020 5:38 AM