Listbox to text

Hi,

I have a listbox with some rows,

Danny Apple 2 Bryan Apple 2 Arief Apple 2 Arief Orange 2 Arief Kiwi 2 Alex mango 2

I am expecting to grouping the item based from the name,

[code] if Listbox1.ListCount>0 then
dim fx as folderitem
fx = GetFolderItem(“data”)
if fx.Exists and fx<>nil then
fx.Delete
end if
dim f as folderitem
dim tisx as TextOutputStream
f = new folderitem(“data”)
tisx = f.CreateTextFile

dim i as integer
for i=0 to listBox1.listcount-1
  tisx.writeline listBox1.cell(i,2)+" "+listBox1.cell(i,1)
  tisx.writeline listBox1.cell(i,0)+EndOfLine
next
tisx.WriteLine " "
tisx.WriteLine " "
tisx.Close

end if[/code]

The result is,

[code]2 Apple
Danny

2 Apple
Bryan

2 Apple
Arief

2 Orange
Arief

2 Kiwi
Arief

2 mango
Alex[/code]

What I am Expecting is

[code]2 Apple
Danny

2 Apple
Bryan

2 Apple
2 Orange
2 Kiwi
Arief

2 mango
Alex
[/code]

I wanted to create the list, while the user is has more than 1 item, its print all first, then the name.

any helps ?

Thanks
Regards,
Arief

Set your data in 3 columns ?

There are few diff. ways to code this.

You can use array or dictionary kind data types to create such a list a then to render to list or anywhere else.

Some steps for example would be:

  • Create data with first list where you have list of all items, then

  • Pass that list to e.g. function which will return array or dictionary kind data type as return value

  • In that function you create unique list like you describe and present in 2nd list by adding or appending item in it with given values where static unique values are person’s and fruits name and amount/counter is dynamic value which can be increased or decreased for N - in your case would be n = n + value from 3rd column and where n starts from 0 for each item in first list and match to person’s and fruits name since each person’s can have more then one fruits in possessions.

  • Use returned array or dictionary kind value to render returned list to listbox or other kind of control.

Above dim. function is just way to make your code more cleaner but also you can handle all that in one go if you like… it’s all about style of coding your app.

Hope it’s helps this.

Hi Mr. Pavlovic,

I don’t familiar to use dictionary or an array, If you do have some code sample, would help me a lot.

thanks
regards,
arief

Given your code I would expect the result you are receiving.

for i=0 to listBox1.listcount-1 tisx.writeline listBox1.cell(i,2)+" "+listBox1.cell(i,1) tisx.writeline listBox1.cell(i,0)+EndOfLine next
You write the name (col 0) for every row in the list box.

Save the name and write it before the name changes.

Arief,

Please take a look examples which comes with Xojo and technical documentation also which comes with Xojo or online regarding to arrays and dictionaries.

If your planning to do coding then you will use them (data types such as array, dictionary…) very often so spending a little time by looking in to it will do a magic for you later.

You have also code samples in technical documentation as well.