Loading from a .dat file into a listbox

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I am working on building a web browser for my program and I have been trying to add a way for the user to add their own favorites. So I have a place where they click Add to Favorites and they are asked for the url of the site they want to add, then it is added to a .dat file, here is the code used for saving the favorites:

Code:
       Saves favorites url to file
        Dim sw As System.IO.StreamWriter = _
    New System.IO.StreamWriter(Application.StartupPath & "\Sidekick Files\Settings\Favorites.dat", True)
        Try
            sw.WriteLine(MyFavoritesURL & vbTab & MyFavoritesURL)
            sw.Flush()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Exclamation, "Error saving new favorites URL. Please contact developer.")
        Finally
            sw.Close()
            sw = Nothing
        End Try

        MessageBox.Show("My Favorites URL Successfully Saved!")

and here is the code that I have to load the favorites into a listbox:

Code:
        Loads Favorites into listbox
        Dim MyFavoritesURL As String
        Dim sr As System.IO.StreamReader = _
    New System.IO.StreamReader(Application.StartupPath & "\Sidekick Files\Settings\Favorites.dat")
        Dim line, vals() As String
        Do
            line = sr.ReadLine
            If line Is Nothing Then Exit Do
            vals = line.Split(vbTab)
            If vals(0) = MyFavoritesURL Then
                lbFavorites.Items.Add(vals(1))
            End If
        Loop

        sr.Close()
        sr = Nothing

Now the problem is that its not loading anything into the listbox.. anyone see a problem with any of that code?

Thanks
 
In your code...
Code:
        Loads Favorites into listbox
        Dim MyFavoritesURL As String
        Dim sr As System.IO.StreamReader = _
    New System.IO.StreamReader(Application.StartupPath & "\Sidekick Files\Settings\Favorites.dat")
        Dim line, vals() As String
        Do
            line = sr.ReadLine
            If line Is Nothing Then Exit Do
            vals = line.Split(vbTab)
            If vals(0) = MyFavoritesURL Then
                lbFavorites.Items.Add(vals(1))
            End If
        Loop

        sr.Close()
        sr = Nothing

...where are you setting the value of MyFavoritesURL? I ran the following code (giving MyFavoritesURL a value) and it worked fine.

Code:
Dim MyFavoritesURL As String = "Test 1"
		Dim sr As System.IO.StreamReader = _
	   New System.IO.StreamReader(Application.StartupPath & "\Favorites.dat")
		Dim line, vals() As String
		Do
			line = sr.ReadLine
			If line Is Nothing Then Exit Do
			vals = line.Split(CType(vbTab, Char))
			If vals(0) = MyFavoritesURL Then
				ListBox1.Items.Add(vals(1))
			End If
		Loop

		sr.Close()
		sr = Nothing
You may just need to make sure that the varible is being set correctly.
 
yes, it works fine now, thanks

I also would like to know if anyone knows how I can remove certain lines from the .dat file, in other words remove favorites?
 
Lanc1988 said:
yes, it works fine now, thanks

I also would like to know if anyone knows how I can remove certain lines from the .dat file, in other words remove favorites?

your ".dat" is just a fancy word for plain text file it looks like.

If you want real capabilities for manipulating data without recreating the wheel, look into XML files. Youd just load your Favorites up like a database and delete a row if you didnt want it.

But to effectively delete a row from a plain text file, you need to write over the existing data - without that line of data.

If you read into a String Array, you have to write the file again, but without that index you dont want.

Easier to do with a Collection or HashTable.

Dim FavoritesHash as new HashTable
FavoritesHash.add(key, object)

so to add:

FavoritesHash.add(FavoriteName, FavoriteAddress)

to delete:

FavoritesHash.delete(FavoriteName)

then to write you do this:

Code:
dim favorite as string
ForEach favorite in FavoritesHash
code to write line... something like:
Write(favorite.key & " " & favorite.Value) 
Remember that .key is the name and .value is the object holding the html address
End For

many more ways to skin a cat :D
 
Back
Top