New topic: 

Populating a listbox

<http://forums.realsoftware.com/viewtopic.php?t=46175>

         Page 1 of 1
   [ 3 posts ]                 Previous topic | Next topic          Author  
Message        Ozzy          Post subject: Populating a listboxPosted: Sat Dec 
08, 2012 3:05 am                         
Joined: Thu Jul 28, 2011 2:45 am
Posts: 28                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                ktekinay          Post subject: 
Re: Populating a listboxPosted: Sat Dec 08, 2012 3:29 am                        
         
Joined: Mon Feb 05, 2007 5:21 pm
Posts: 295
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                ktekinay          Post subject: 
Re: Populating a listboxPosted: Sat Dec 08, 2012 3:41 am                        
         
Joined: Mon Feb 05, 2007 5:21 pm
Posts: 295
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             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 3 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to