[2005] how can i loop this text..
hi all again.
when i am loading my favourites(in my browser) i do the method below.
is there a loop that will do all of the below code?
--- in the form the button order is button1,2,5,3,4,7, that is why the order is the way it is----------------
<code>
Private Sub favourites_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim fileExists As Boolean
fileExists = My.Computer.FileSystem.FileExists("c:\Program files\WeBrowz\favourite1.text")
If fileExists = True Then
Dim fileContents1 As String
fileContents1 = My.Computer.FileSystem.ReadAllText("c:\Program files\WeBrowz\favourite1.text")
Button1.Text = fileContents1
Dim fileContents2 As String
fileContents2 = My.Computer.FileSystem.ReadAllText("c:\Program files\WeBrowz\favourite2.text")
Button2.Text = fileContents2
Dim fileContents3 As String
fileContents3 = My.Computer.FileSystem.ReadAllText("c:\Program files\WeBrowz\favourite3.text")
Button5.Text = fileContents3
Dim fileContents4 As String
fileContents4 = My.Computer.FileSystem.ReadAllText("c:\Program files\WeBrowz\favourite4.text")
Button3.Text = fileContents4
Dim fileContents5 As String
fileContents5 = My.Computer.FileSystem.ReadAllText("c:\Program files\WeBrowz\favourite5.text")
Button4.Text = fileContents5
Dim fileContents6 As String
fileContents6 = My.Computer.FileSystem.ReadAllText("c:\Program files\WeBrowz\favourite6.text")
Button7.Text = fileContents6
End If
<Code>
cheers,
patrick
Re: [2005] how can i loop this text..
1) "If fileExists = True Then" takes more code, and more time to run, than "If fileExists Then"
2) Dim all your strings at the top. It's cleaner looking code, and Dim statements are executed unconditionally.
3) If you define your string as an array of string, you can build the file name based on the loop counter and your string is ArrayName(<loop counter>).
Re: [2005] how can i loop this text..
If you are linking buttons to files, I would load them dynamically at runtime instead of having a set number of buttons already on the form. Then just set the Tag property of each dynamically loaded button to contain the contents of the files. Also, I'd use a For Each loop on IO.Directory.GetFiles to dynamically load the favorites.
Or actually, if you are making a browser favorites list, I would try to go with a single formatted document that contains all the favorites and then just load the single file. It'd probably be faster than opening and closing a lot of files on the hard drive, if you just open 1 file and loop through it line by line.
And a side note, if you want to post code, use the [vbcode] tags so your code is easy to read in the forums. :)
Re: [2005] how can i loop this text..
Quote:
Originally Posted by supertotallyawesome
Or actually, if you are making a browser favorites list, I would try to go with a single formatted document that contains all the favorites and then just load the single file.
how do i do that? i thought that it was possible but i didnt know how to do it. if you want check out my program it is here
http://www.vbforums.com/showthread.php?t=412859
cheers
patrick :wave:
Re: [2005] how can i loop this text..
Quote:
Originally Posted by Gameunreal
Just do a really simple formatted file named something like "favorites.txt":
Code:
Website Title|http://www.website.com
Super Cool Site|http://www.supercool.com
Google|http://www.google.com
and to read it, use a StreamReader or TextReader to read the file in:
VB Code:
Dim Temp As String TempA() As String
Dim sr As New StreamReader("favorites.txt")
Do While sr.Peek <> -1
Temp = sr.ReadLine()
TempA = Temp.Split("|")
' here is where you would create the dynamic control
' whether it be a button or a menu item, then
YourDynamicItem.Text = TempA(0)
YourDynamicItem.Tag = TempA(1)
Loop
And just so you know, your zip file is missing a lot of image files in the resource folder. I had to create a bunch of fake ones to coerce the program into compiling.
Re: [2005] how can i loop this text..
Should have just debugged it, or downloaded mine: http://www.freewebs.com/tegadigital/. And I'm not saying mine is better but just saying if you wanted to try it out go ahead....I'm uploading a better version over the weekend.