|
-
Feb 7th, 2008, 10:07 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Collection In ListBox
In a web browser app, the URLs that users add to their Favorites are populated in a text file in the following format:
"Google","http://www.google.com"
"Yahoo","http://www.yahoo.com"
"MSN","http://www.msn.com"
i.e. each entry in the text file contains 2 parts - the title of the web page & its URL with comma as the delimiter. Users can also delete their favorites. To do this, there is a Delete Favorites menu item.
When the Delete Favorites menu item is clicked, a new Form opens up wherein all the favorites are listed in a ListBox. There are 2 CommandButtons as well - Delete & Cancel. When a user selects a favorite from the ListBox & clicks the Delete button, that favorite gets deleted from the ListBox. After this the rest of the contents i.e. the edited list of favorites are re-populated in the text file. Users can select multiple favorites from the ListBox to delete more than one favorite at the same time. This is how I am populating the ListBox:
Code:
Private col As Collection
Private Sub Form_Load()
Call ReadFavorites
End Sub
Private Sub ReadFavorites()
Dim iFile As Integer
Dim strURL As String
Dim strTitle As String
Dim FileName As String
iFile = FreeFile
FileName = App.Path & "\Favorites.txt"
If (Dir$(FileName, vbHidden) = vbNullString) Then
Exit Sub
End If
Set col = New Collection
Open FileName For Input As #iFile
Do While Not EOF(iFile)
Input #iFile, strTitle, strURL
lstFavorites.AddItem strTitle
col.Add strURL
Loop
Close #iFile
End Sub
This is the code for deleting the favorites from the ListBox:
Code:
Private Sub cmdDelete_Click()
Call DeleteFavorites
End Sub
Public Sub DeleteFavorites()
Dim frm As Form
Dim strURL As String
Dim FileName As String
Dim strTitle As String
Dim i As Integer
Dim iFile As Integer
If (vbYes = MsgBox("Are you sure you want to delete the Favorites?", vbYesNo + vbQuestion)) Then
For i = lstFavorites.ListCount - 1 To 0 Step -1
If (lstFavorites.Selected(i) = True) Then
' Debug.Print i
lstFavorites.RemoveItem i
col.Remove i + 1
End If
Next i
iFile = FreeFile
FileName = App.Path & "\Favorites.txt"
SetAttr FileName, vbNormal
Open FileName For Output As #iFile
For i = 0 To lstFavorites.ListCount - 1
Write #iFile, lstFavorites.List(i), col.Item(i + 1)
Next i
Close #iFile
SetAttr FileName, vbHidden
'more than 1 browser window may be open
Open FileName For Input As #iFile
Do While Not EOF(iFile)
Input #iFile, strTitle, strURL
For Each frm In Forms
If (frm.Name = frmOwner.Name) Then
For i = frm.mnuFavSites.UBound To 1 Step -1
Unload frm.mnuFavSites(i)
Next
End If
Next
Loop
Close #iFile
Set col = Nothing
End If
End Sub
Now when I delete any Favorite (selecting only 1 Favorite from the ListBox) for the first time, it gets deleted successfully but when I try to delete any subsequent favorites from the ListBox - be it just one favorite or multiple - VB generates the following error pointing to the red colored line shown in the above code:
Object variable or With block variable not set
Can someone please tell me why is this error getting generated & how do I overcome it?
ARPAN
IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!
NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!
PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?
-
Feb 7th, 2008, 10:57 AM
#2
Re: Collection In ListBox
In DeleteFavourites, the col variable is set to nothing. Delete that line.
-
Feb 7th, 2008, 01:48 PM
#3
Thread Starter
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|