In a Web Browser project, whichever web page a user has navigated to, I am adding the titles of the web pages along with their respective URLs in a text file. I am then populating 2 Collections - one with the title of the web page & the other one with the URL of the web page. This is how I am doing it:
VB Code:
  1. Private Sub wWeb_TitleChange(ByVal Text As String)
  2.     Dim iFile As Integer
  3.     Dim FileName As String
  4.     Dim colTitle As Collection
  5.     Dim colURL As Collection
  6.  
  7.     Set colTitle = New Collection
  8.     Set colURL = New Collection
  9.  
  10.     iFile = FreeFile
  11.     FileName = App.Path & "\SessionHistory.txt"
  12.  
  13.     Open FileName For Input As #iFile
  14.         Do While Not EOF(iFile)
  15.             Input #iFile, strTitle, strURL
  16.             colTitle.Add strTitle, strTitle
  17.             colURL.Add strURL, strURL
  18.         Loop
  19.     Close #iFile
  20. End Sub
There is another function (not shown) which loads a menu with these web pages. The web page the user currently is in is checked in the menu. A user can navigate in 2 ways - either by clicking a link or by pressing the Back/Forward buttons. If the Back or Forward button is pressed then a Public Boolean blnNavButtons is set to True & in the BeforeNavigate2 event function:
VB Code:
  1. Private Sub wWeb_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
  2.     If (blnNavButtons = True) Then
  3.         blnLinkClick = False
  4.     Else
  5.         blnLinkClick = True
  6.     End If
  7. End Sub
Assume that a user goes to Page1, clicks a link & goes to Page2. In Page2, he again clicks a link which takes him to Page3 & finally in Page3, he clicks a link which takes him to Page4. Note that while he is navigating, the menu is simultaneously getting loaded with Page1, Page2, Page3 & Page4 which is now checked.

Right now, the user is in Page4. Now assume that he clicks the Back button & goes to Page3. Again he clicks the Back button & goes to Page2. Again he clicks the Back button which takes him to Page1. Now if the user clicks a link in Page1 which takes the user to Page5, I want Page2, Page3 & Page4 to get deleted from the Collections as well as from the menu. This is how I have done it (continuing wWeb_TitleChange where it has been left):
VB Code:
  1. Private Sub wWeb_TitleChange(ByVal Text As String)
  2.     ..........
  3.     ..........
  4.     ..........
  5.     Close #iFile
  6.  
  7.     For i = 1 To colURL.Count
  8.         If (colURL.Item(i) = wWeb.LocationURL) Then
  9.             If (colURL.Count > i) Then
  10.                 If (blnLinkClick = True) Then
  11.                     For j = i + 1 To colURL.Count
  12.                         colURL.Remove j
  13.                         colTitle.Remove j
  14.                         Unload mnuSessionHistory(j - 1)
  15.                     Next j
  16.  
  17.                     FileName = App.Path & "\SessionHistory.txt"
  18.                     Open FileName For Output As #iFile
  19.                         For j = 0 To mnuSessionHistory.UBound
  20.                             Write #iFile, mnuSessionHistory(j).Caption, mnuSessionHistory(j).Tag
  21.                         Next
  22.                     Close #iFile
  23.                 End If
  24.             End If
  25.         End If
  26.     Next i
  27. End Sub
The above code works fine but a problem creeps under the following circumstance:

User goes to Page1, clicks a link & goes to Page2, clicks a link & goes to Page3, clicks a link & goes to Page4. The menu now looks like this:

Code:
Page1
Page2
Page3
Page4
Page4 is checked. Now in Page 4, there is a link which takes the user back to Page1. So when this link is clicked, all the menu items get deleted & only Page1 remains in the menu where as I want the menu to look like this:

Code:
Page1
Page2
Page3
Page4
Page1
With Page1 checked.

What modifications are needed in the above code to do this?