Xojo / RS Export Listbox

I’m going to start adding some software development tips and tricks and other useful bits and bobs to this site with the hope that other people can learn and benefit from my knowledge and experience in the same way that I have learnt, and continue to learn, from others.

I’m not sure yet if there will be a particular focus or even how often I’ll post – but that will become apparent over time.

I’ll start with this; a simple method, written in Xojo / REALStudio, to take the contents of a Listbox and generate a Tab Delimited export to a text file for every row and every column in the Listbox. In this example the Listbox is named lstTemp and you will have previously populated it with your data.

  ‘Export the List as Tab Delimited
  if lstTemp.ListCount = 0 then Return
  dim f as FolderItem
  dim t as TextOutputStream
  dim intRow, intColumn as Integer = 0
  f = GetSaveFolderItem(“txt”,”Export.txt”)
  if f = Nil then Return
  t = f.CreateTextFile
  for intRow = 0 to lstTemp.ListCount-1
    for intColumn = 0 to lstTemp.ColumnCount-1
      t.Write ConvertEncoding(lstTemp.Cell(intRow,intColumn),Encodings.SystemDefault)
      t.Write chr(9)
    next
    t.Write EndOfLine
  next
  t.Close

Originally when I published this on a previous blog, I had some feedback from a friend of mine and fellow Developer, Eric Gibbon of MacTasks. This is what he had to say:

The code is quicker if you store the lstTemp.ListCount-1 and lastTemp.ColumnCount-1 in local variables, as for every loop iteration your code calls the ListBox to get its ListCount or ColumnCount value and then subtracts one. However if these values are stored in local variables this repeating does not happen. For a small list this will not matter but for larger ones there will be a performance difference. So the code would be like:

dim nRows as Integer = lstTemp.ListCount – 1
dim nCols as Integer = lstTemp.ColumnCount – 1
for intRow = 0 to nRows
  for intColumn = 0 to nCols
  next
  …
next

 

It would also be nice to add a parameter to your routine for “Include Headings” and write the ListBox headings to the file first if this parameter is True.

I hope this helps someone.

Blatant Advertising: New Xojo / REALStudio DataViewer Component

blog_footer.png

Products
Affiliate Links