gettingstarted
[Top] [All Lists]

Re: Reading in text from a file into a ListBox with 2 columns

To: gettingstarted at lists dot realsoftware dot com
Subject: Re: Reading in text from a file into a ListBox with 2 columns
From: Emile Schwarz <emile dot a dot schwarz at wanadoo dot fr>
Date: Sun, 30 Jan 2005 11:16:10 +0100
Delivered-to: gettingstarted at lists dot realsoftware dot com
References: <20050130024126 dot E1FDC6D73CF at lists dot realsoftware dot com>
Hi,

why are you using:

cr=Chr(13)+Chr(10)

instead of EndOfLine ? (check that instruction description before answering).

In fact, why don't you use WriteLine instead of Write ?


You set a name for the window (Players), but you do not change the ListBox name (ListBox1). Why don't you give it a name (say Scores) ?


Why, instead of:
>    For n=0 to Players.ListBox1.ListCount-1
>      s=Players.ListBox1.Cell(n,0)+","+Players.ListBox1.Cell(n,1)+cr

don't you use:

Dim LB As ListBox

LB = Players.ListBox1

For n=0 to LB.ListCount-1
  s = LB.Cell(n,0) + "," + LB.Cell(n,1) + EndOfLine


Why, instead of counting each time you start a new loop, you do not use a 
variable:

Dim RowCnt As Integer

RowCnt = LB.ListCount - 1

For n=0 to RowCnt



What is that funny n ?
> next n


And, at last (for me and at this time in the day), don't you use:

t.Write LB.Cell(n,0) + "," + LB.Cell(n,1) + EndOfLine


And now, including all my changes:

  Dim myPrefFI  As FolderItem
  Dim myPrefTOS As TextOutputStream
  Dim LB        As ListBox
  Dim RowCnt    As Integer
  Dim Idx       As Integer

  // Typing 'energy' saver
  LB = Players.Scores

  // Get the number of Rows
  RowCnt = LB.ListCount - 1

  // Get a FolderItem
  myPrefFI = PreferencesFolder.Child("MyPrefsFileName.plist")
  If myPrefFI = Nil Then Return // *

  // Get a TextOutputStream
  myPrefTOS = myPrefFI.CreateTextFile

  // Loop inside the ListBox
  For Idx = 0 To RowCnt
    // Write a line for a Row
    myPrefTOS.Write LB.Cell(Idx,0) + "," + LB.Cell(Idx,1)

    // Simple error checkings
    If myPrefTOS.LastErrorCode <> 0 Then Exit // *
  Next

  // Close the TextOutputStream
  myPrefTOS.Close


Nota: the above code compiles fine, but I do not execute it (I only build the application).


WARNING: Next n compiles and this is new for me; the embeded Language Reference do not talks about this (usually, only comments can be placed after Next - same line -).


Paul, if there is something in my code that you do not understand, check the language reference (or ask here). Usually, I do not like to give code as is because people can use it without understanding it (it seems to me that this is what you've done previously); the next time you need it (for saving the Scores for example), you have to ask.

Also, your code works for a two columns ListBox (is not dynamic, what will happens if you have a third|forth column ?)

You use a comma as a Column delimiter. That is good, but are-you sure this is good ? (OK, I prefer to use a tab [Chr(9)], but this is just me, my preference).


At last, did you make a search in the List archive ?
In the previous week(s), I share a piece of code to Import / Export strings to / from File / ListBox (in other words, I provided two dynamic "Import from file into multi-column ListBox" and "Export from a multi-column ListBox to a file" methods).


HTH,


Emile



* Simple file I/O error testings. You can do better code.



gettingstarted-request at lists dot realsoftware dot com wrote:
Subject: Re: Reading in text from a file into a ListBox with 2 columns
From: Paul Young <youngpr at myactv dot net>
Date: Sat, 29 Jan 2005 17:11:55 -0500

Phil,

Nice coding.  Thanks.  Can you improve on my code to save the info per:

   dim f as FolderItem
   dim t as TextOutputStream
   dim s as String
   dim cr as String
   dim n as Integer
   cr=Chr(13)+Chr(10)
   f=PreferencesFolder.Child("filename.plist")
   t=f.CreateTextFile
   For n=0 to Players.ListBox1.ListCount-1
     s=Players.ListBox1.Cell(n,0)+","+Players.ListBox1.Cell(n,1)+cr
     t.Write s
   next n
   t.close




_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>