Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Jun 23, 2018 1:37 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Populating a listbox
PostPosted: Sat Dec 08, 2012 3:05 am 
Offline

Joined: Thu Jul 28, 2011 2:45 am
Posts: 32
I have a three column list box which I want to populate by entering a two column csv file. I want to enter the csv data into columns two and three. I have got code from the forum that works well when importing into a two column listbox. I modified the code so that I thought it would read into the second and third columns but I get the following error message when I try it with the three column list box:

"An exception of class Outofboundsexception was not handled."

This is my code:

Dim f As FolderItem
dim tis as TextInputStream
dim s as string
dim i as integer
dim fields() as string

'show standard file selector
f = GetOpenFolderItem("" )

'open the file
tis = f.OpenAsTextFile
if tis=nil then 'failed?
MsgBox("The file could not be opened.")
end if

'read file into grid
while not tis.EOF
ListBox1.AddRow ""
s=tis.ReadLine
fields=Split(s,",")
for i=1 to ListBox1.ColumnCount-1 //this is the line I changed
ListBox1.Cell(ListBox1.ListCount-1,i)=Trim(fields(i))
next
wend

tis.Close

Can anybody see where I have gone wrong?


Top
 Profile  
Reply with quote  
 Post subject: Re: Populating a listbox
PostPosted: Sat Dec 08, 2012 3:29 am 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
First, it's good that you are checking tis for nil, but your code proceeds anyway. After the MsgBox, you should "return" to avoid the rest of the code. That's not your problem here, but might be an issue down the road.

I assume that ListBox1.ColumnCount = 3? Let's assume so. Also assuming that each line really splits into two text fields (which you should ensure anyway), your indexes are wrong. You want to put fields( 0 ) into Column 1, but you are putting fields( 1 ) into Column 1, and then fields( 2 ) (which is out of bounds) into Column 2.

Try this:
Dim f As FolderItem
dim tis as TextInputStream
dim s as string
dim i as integer
dim fields() as string
dim lastIndex as integer = ListBox1.ColumnCount - 1

'show standard file selector
f = GetOpenFolderItem("" )

'open the file
tis = f.OpenAsTextFile
if tis=nil then 'failed?
MsgBox("The file could not be opened.")
return
end if

'read file into grid
while not tis.EOF
ListBox1.AddRow ""
s=tis.ReadLine
fields=Split(s,",")
redim fields( 1 ) // Ensure two fields
for i=1 to lastIndex
ListBox1.Cell(ListBox1.LastIndex,i)=Trim(fields( i - 1 ))
next
wend

tis.Close

Note that ListBox1.LastIndex will give you the index of the row you just added. Another way to do this without the loop (since you are only adding two columns):
// ...
while not tis.EOF
s=tis.ReadLine
fields=Split(s,",")
redim fields( 1 ) // Ensure two fields
dim field1 as string = Trim( fields( 0 ) )
dim field2 as string = Trim( fields( 1 ) )
ListBox1.AddRow( "", field1, field2 )
wend

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Populating a listbox
PostPosted: Sat Dec 08, 2012 3:41 am 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
If each field doesn't really have to be trimmed, this is even better:
// ...
while not tis.EOF
s=tis.ReadLine
s = "," + s // Ensure a leading, blank field
fields=Split(s,",")
ListBox1.AddRow( fields )
wend

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: Populating a listbox
PostPosted: Sat Dec 08, 2012 8:23 pm 
Offline

Joined: Thu Jul 28, 2011 2:45 am
Posts: 32
Thanks Kem, I works well with your code. Thanks also for the additional advice regarding the "return" statement. I have added it in.


Ozzy


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group