I have created an application which creates multiple webbrowser controls at runtime...now i want to clean up memory when these webrowsers are deleted see the code below please let me know how can i do it(i have tried dispose method but no luck)

Creation on dynamic web browsers on button click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i = 0
        Dim y = 0
        For i = 0 To (ListBox1.SelectedIndices.Count - 1)
            Dim t As System.Windows.Forms.AnchorStyles = AnchorStyles.Top
            Dim b As System.Windows.Forms.AnchorStyles = AnchorStyles.Bottom
            Dim r As System.Windows.Forms.AnchorStyles = AnchorStyles.Right
            Dim s = ListBox1.Items(ListBox1.SelectedIndices.Item(i)).ToString()
            Dim TT As New TEST_TABS
            TT.wb = New WebBrowser
            TT.wb.AllowNavigation = True
            TT.wb.Location = New Point(0, 0)
            TT.wb.Size = TabControl1.Size
            TT.wb.Name = s+"e"
            TT.wb.ScriptErrorsSuppressed = True


            ' TT.wb.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            '   Or System.Windows.Forms.AnchorStyles.Left) _
            ' Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            TT.pb = New ProgressBar
            TT.pb.Name = s + "p"
            TT.pb.Location = New Point(y, 0)
            TT.pb.Size = New Size(125, 24)
            'TT.pb.Size = TabControl1.Size
            TT.pb.Visible = True
            y += 126
            Dim tab_page As New TabPage
            tab_page.Name = s
            tab_page.Text = s
            tab_page.Font = New System.Drawing.Font("Calibri", 22)

            TabControl1.SelectedIndex = i

        Next i
        i = 0

        For Each tp As TabPage In TabControl1.TabPages


        Button1.Enabled = False

        Button2.Enabled = True
    End Sub

I want to remove memory used by these dynamic webbrowsers on below click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        For Each tp As TabPage In TabControl1.TabPages

            Me.SplitContainer1.Panel2.Controls.Item(tp.Name.ToString + "p").Dispose()

        Button1.Enabled = True


    End Sub

Below is the TEST_TABS class

Public Class TEST_TABS
    Public WithEvents wb As New WebBrowser
    Public WithEvents pb As New ProgressBar

    Dim page_counter As Integer = 0

    Private Sub wb_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted
        ' MsgBox(page_counter)
        page_counter += 1

        If wb.DocumentText.Contains(WindowsApplication1.My.Resources.Resource1.loginpage) Then

            wb.Document.GetElementById("username-id").SetAttribute("value", Form1.ComboBox1.SelectedItem.ToString)
            wb.Document.GetElementById("pwd-id").SetAttribute("value", Form1.ComboBox1.SelectedItem.ToString)

        End If
        If page_counter = 1 Then

        End If
    End Sub

    Private Sub wb_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles wb.ProgressChanged
        wb.Size = Form1.TabControl1.Size

        If (e.CurrentProgress >= 0 And e.CurrentProgress < 100) Then

            pb.Maximum = 100
            pb.Value = Convert.ToInt32(e.CurrentProgress)

        End If
    End Sub
End Class

Recommended Answers

All 6 Replies

I came across this piece of code once while trying to solve a similar problem. See if this helps.

Private Sub releaseObject(ByVal obj As Object)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    End Try

End Sub

According to the person who posted it, it is supposed to release the passed object then force garbage collection to run to reclaim the space.

Hi Jim,

Thanks for your releaseobject() method...although i was not able to pass the dynamic objects created (TT.wb and TT.pb)under Button1.click method into releaseObject()

I have called GC.collect() just as i dispose these dynamic controls and Yippi :icon_cheesygrin: my memory was regained on runtime

For Each tp As TabPage In TabControl1.TabPages
            Me.SplitContainer1.Panel2.Controls(tp.Name.ToString + "p").Dispose()

I have a query... Q: How can we access the objects instance created at runtime in a particular method (in my case under Button1.click) FROM any other menthod or outside of the creator method ???

Public Class Form1
    Public mytextbox as New TextBox
End Class

Public Class Form2
End Class

Well thats accessing objects accross two classes and thats fine...

I want to access some things like below

Public class something

Sub one()

Dim TB as New textBox

End Sub

Sub two()

'How to access TB here ?? or from anywhere in app domain

End Sub

End class

Same thing. Declare it at the class level and it will have class scope. If you are only using it inside the class then you can make it private.

Add a "cool" Parameter to your Sub.

Sub two(byval selCoolTextBox as TextBox)

   'How to access TB here ?? or from anywhere in app domain
End Sub

And call it as:

private sub someBtn.Click
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.