Lanc1988 Posted August 5, 2004 Posted August 5, 2004 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: '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: '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 Quote
Mothra Posted August 5, 2004 Posted August 5, 2004 In your 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. 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. Quote Being smarter than you look is always better than looking smarter than you are.
Lanc1988 Posted August 5, 2004 Author Posted August 5, 2004 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? Quote
Denaes Posted August 5, 2004 Posted August 5, 2004 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. You'd just load your Favorites up like a database and delete a row if you didn't 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 don't 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: 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 Quote
Lanc1988 Posted August 5, 2004 Author Posted August 5, 2004 k, thanks everything working perfectly Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.