ListBox Prefs 
Author Message
 ListBox Prefs

Hi again,
My question this time is a little different.  I also want my app to
remember the contents of a listbox, and all the code i've tried so far
hasn't worked.  I've created a text file in the same location as the
app, but it's always empty.  I want the contents of each line of the
listbox, written to my text file upon quitting, and placed back into the
listbox, upon opening.  
What should the line of code be.  I have something like this in my
WriteList method:

  dim file as TextOutputStream
  dim thefile as FolderItem
  dim x,y  as integer
  dim line as string
  ListURL.setfocus //Select listbox1
  thefile = GetFolderItem("Prefs") //file to save
  file = thefile.CreateTextFile  //Create file
  for x = 0 to ListURL.listcount-1  //for x=0 to no. of rows
    ListURL.listindex=x  //Select current row
    //It's the line above, and what ever else that should go here, where
               I am stuck
  next
  file.close

I'm also a little puzzled on the line of code that puts all of the
contents back into the listBox upon opening the app, (my ReadList
method).  

Thank You once again in advance,
Mark Bee.  



Tue, 04 May 2004 11:00:42 GMT  
 ListBox Prefs

Quote:
>    //It's the line above, and what ever else that should go here, where
>               I am stuck

You need to write the listbox's contents into the text file.

You write something into the text file with the textOutputStream's "write" or
"writeline" method. For this particular use, "writeLine" is the easier way to
go.

You fetch the line to write from teh listbox's list property.

So, inside your loop, you need to do this:

file.writeLine ListURL.list(x)

You'll do the opposite to read the text file back in; you'll need to open your
file as a textInputStream, then create a loop. In the loop, you'll use the
textInputStream's readLine method to fetch what has to go into the listbox, and
add it to the listbox. Keep doing this until the text file's eof property is
true (indicating you've read the entire file).

Hope that helps...

--
"Quand la morale triomphe, il se passe des choses tres vilaines."
Literature. Art. Photography. Forums. Shareware. Kink. Sex.
All at: http://www.xeromag.com/franklin.html



Tue, 04 May 2004 11:53:18 GMT  
 ListBox Prefs

Quote:

> Hi again,
> My question this time is a little different.  I also want my app to
> remember the contents of a listbox, and all the code i've tried so far
> hasn't worked. [..]

To save:

dim ts as TextOutputStream
dim f as FolderItem
dim i, j as Integer

f = GetFolderItem("filename")
if f <> nil then
  ts = f.CreateTextFile
  if ts <> nil then
    j = ListBox.ListCount - 1
    for i = 0 to j
      ts.WriteLine ListBox.List(i) // or ListBox.Cell(x,y)
    next
    ts.Close
  end if
end if

To load:

dim ti as TextInputStream
ListBox.DeleteAllRows
f = GetFolderItem("filename")
if f <> nil then
  ti = f.OpenAsTextFile
  if ti <> nil then
    while not ti.EOF
      ListBox.AddRow ti.ReadLine
    wend
    ti.Close
  end if
end if

HTH.

--
"In a world full of lies/That tug at the truth
 I'm taking no sides/Now I recognize you..." -DS



Tue, 04 May 2004 13:49:21 GMT  
 ListBox Prefs
Thanks, but I'm now having different problems.  When I run my app,
without the readList method activated on opening, the app might write
the contents of the listBox, (I think), and when I run my app with the
readList method, it doesn't start properly.  I just get the watch
cursor, and nothing happens.  I suspect there's something wrong in the
code.  Here's what I've got:

For the WriteList method, (upon quitting):

dim file as TextOutputStream
dim thefile as FolderItem
dim x,y  as integer
dim line as string
ListBox1.setfocus
thefile = GetFolderItem("List Box Stuff")
file = thefile.CreateTextFile
  for x = 0 to Listbox1.listcount-1
    file.WriteLine ListBox1.list(x)
  next
 file.close
 listbox1.deleteallrows    <- not sure about this line
 Readprefs         <- or this line

For the ReadList method, (upon opening):

  dim file as TextInputStream
  dim thefile as FolderItem
  dim line as string
  Dim x As Integer
  listbox1.deleteallrows  <- not sure about this line
  thefile = GetFolderItem("URL's")
  if thefile.exists then
    file = thefile.OpenAsTextFile
    while not file.EOF
      for x = 0 to listBox1.listcount-1
        listBox1.list(x)=file.ReadLine     <- Is this loop right?
      next
    wend
    file.close
  end if

Basically, it won't ever read, (if it starts up with the ReadList method
on), and 'sometimes' it will save the ListBox contents.  

There's probably something wrong with the code that's evident, but I
don't see it.  

Thanks again,
Mark Bee.  



Tue, 04 May 2004 14:25:53 GMT  
 ListBox Prefs

Quote:
> file.close
> listbox1.deleteallrows    <- not sure about this line
> Readprefs         <- or this line

If you're doing this on quit, it should not be necessary to read your prefs
again.

Quote:
> if thefile.exists then
>    file = thefile.OpenAsTextFile
>    while not file.EOF
>      for x = 0 to listBox1.listcount-1
>        listBox1.list(x)=file.ReadLine     <- Is this loop right?
>      next
>    wend
>    file.close

That loop is wrong. Instead, do it this way:

if thefile.exists then
  file = thefile.OpenAsTextFile
  x=0
  while not file.EOF
      listBox1.list(x)=file.ReadLine
      x=x+1
   wend
   file.close

It might also be a good idea to check that file<>nil after you open the file...

--
"Quand la morale triomphe, il se passe des choses tres vilaines."
Literature. Art. Photography. Forums. Shareware. Kink. Sex.
All at: http://www.xeromag.com/franklin.html



Wed, 05 May 2004 01:32:27 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. RBConfigurableFormatter user prefs not initialized in SP3

2. OSX and that Prefs MenuItem!

3. Saving/reading checkbox states in prefs causes type mismatch

4. OS X Prefs File - How to create

5. save prefs( colors )

6. Creating prefs pane

7. prefs.js question

8. RubyGarden Poll -- setting prefs.

9. Live update of prefs for Tkinter applications?

10. IDLE Color Prefs

11. Listbox background paint and *my* listbox bug

12. Tk Listbox bindings (was: Tk Listbox Question)

 

 
Powered by phpBB® Forum Software