Hi, Paul,
One of the ideas behind object oriented programming is that objects should
know what to do with their own contents. So, rather than reaching into a
ListBox, extracting the contents of the various cells, and formatting them
for output to a text file, you should be asking the listbox to provide you
with a string that is already formatted to be saved in a text file.
You should create a generic, reusable ListBox class that contains some basic
listbox functionality, such as responding to arrow keys, File:Copy menu,
etc. Among its repertoire should be a function that spits out a row's
contents as a concatenated string, suitable for text file operations, or for
passing to a Window to populate EditFields. Similarly, there should me a
method that recieves a delimited text string and parses it to populate a
ListBox row's cells.
Make a new class with ListBox as it's super. Create a Function as follows.
It is generic; it doesn't not matter how many columns there are.
Function contatenateRow(row As Integer) As String
Dim col As Integer
Dim rowTxt As String = Cell(row, col) // col is zero (0) here
For col = 1 To ColumnCount - 1
rowTxt = rowTxt + HT + Cell(row, col)
Next
Return rowTxt
End Function
You can use 'comma' as a delimiter if you want... but be advised, it is
generally a bad choice. Commas show up in ordinary text. If some of the
ListBox cells contain commas, your text file ends up in a condition which is
known (in technical circles) as 'boogered-up', or, in extreme cases,
'FUBAR'. It is usually better to use a TAB, since tabs makes it easier to
make sense out of your data when examining it in a text editor. If TAB is
not a viable choice for some reason, use a 'pipe' [ | ], since that doesn't
show up in ordinary text... or some other character that you know won't show
up in the data.
Depending on what you're doing, it might make sense to include 'delimiter'
as a parameter in your function:
Function contatenateRow(row As Integer, delimiter As String) As String
Dim col As Integer
Dim rowTxt As String = Cell(row, col) // col is zero (0) here
For col = 1 To ColumnCount - 1
rowTxt = rowTxt + delimiter + Cell(row, col)
Next
Return rowTxt
End Function
This is a good case to illustrate RB's ability to specify defaulted
parameters. Done this way, the delimiter is always a 'tab', unless you
override it:
Function contatenateRow(row As Integer, delimiter As String = HT) As String
Now, when you want to save ListBox contents to a text file, do this:
If ListBox1.ListCount < 1 Then
Beep
MsgBox "The ListBox is empty. Nothing to save."
Return
End If
f = PreferencesFolder.Child("filename.plist")
t = f.CreateTextFile
If t = nil Then
MsgBox "File not opened. Nothing saved."
Return
End If
Dim row As Integer
// Write the first row to file
t.WriteLine ListBox1.concatenateRow(row) // row is zero (0) here
// Write the remaining rows, if any... delimited
For row = 1 To ListCount - 1
t.WriteLine EndOfLine + ListBox1.concatenateRow(row)
next
t.close
P.
**************************************
on 1/29/05 5:11 PM, Paul Young at youngpr at myactv dot net wrote:
> 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
>
> Paul
_______________________________________________
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>
|