Read and fille listBox from txt file

Hi,
I use this code to populate listbox from txt file.
I try to fill only tue second column, the first is has fixed label.
in listBox Open event the first column is filled by following lines

For i = 0 to 360 step 15
LBCompas.AddRow(format(i,“000”)+“°”)
Next

The second column must be filled from file text by code

Dim f As FolderItem
Dim t as TextOutputStream
dim strDateTime, strDate, strTime as string
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i , last As Integer
dim strtxt(5) as string

f = GetOpenFolderItem(“Mag.txt”) //defined as a FileType
If f <> Nil And f.Exists Then
Dim coma As String = ChrB(44)
Dim ligne as String

textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.UTF8 //strings are MacRoman
      
Ligne= TextInput.ReadLine
    
While Not textInput.EOF
  
  rowFromFile = textInput.ReadLine
  
  // Set
  If WinMagDev.LBCompas.ColumnCount < CountFields(rowFromFile, coma) Then
    WinMagDev.LBCompas.ColumnCount = CountFields(rowFromFile, coma)
  End If
  
  WinMagDev.LBCompas.AddRow("")
  For i =1 To CountFields(rowFromFile, coma)
    oneCell = NthField(rowFromFile, coma, i)
    WinMagDev.LBCompas.Cell(WinMagDev.LBCompas.ListCount-1, i-1) = oneCell
  Next
Wend

textInput.Close

End If

Return True

When the first column (ListBox Open) is running, the second column (dev) is not populate from the file text.
Populate only when list box LBComps.AddRow() is disactivated.

list box screenshot empty
ListBx

Apparently my answer is lost in the ether…

Your problem is i-1 here:

Cell(WinMagDev.LBCompas.ListCount-1, i-1)

Set your column count to 25 and run your code for confirmation. You will see the values loaded from the text files in Row 0, Column 1 to Row 24, Column 24 (I think).

Shouldn’t that be

WinMagDev.LBCompas.CellValueAt( i-1, 1) = oneCell

? Cell is deprecated and replaced by CellValueAt but the bug is in the parameters.

1 Like

That is nearly what I wrote.

I think Djamel do not use API2 (he run an old Xojo version)

Whether Cell or CellValueAt is used, the first parameter should be the desired row (which is i-1) and the second should specify the column (1, i.e. the second column).

Yes !

Emile,
The first column is fixed does not change label 000° … 360°
only the the dev header column must be filled 20 values.
file text looks like this
,1
,2
,3
.
.
.
,24

Coma is used because the code is wrote to read a row (line)with several columns .

my xoxjo is 2015r1

We give you the answer (IMH0):

    WinMagDev.LBCompas.Cell(WinMagDev.LBCompas.ListCount-1, 1) = oneCell

Note that I removed the i-1 and replaced it with 1.

Column(1) in Xojo 2015r1 is Column #2

Using i-1 will add text n Column 1-1, 2-1,3-1,4-1,5-1, and so on…

SET THE COLUMNCOUNT TO 24 AND RUN YOUR PROJECT TO UNDERSTAND (enlarge the ListBox so it will be able to display 24 coluns…)

  WinMagDev.LBCompas.AddRow("")
  For i =1 To CountFields(rowFromFile, coma)
    oneCell = NthField(rowFromFile, coma,i)
    //WinMagDev.LBCompas.Cell(WinMagDev.LBCompas.ListCount-1, i-1) = oneCell
    WinMagDev.LBCompas.Cell(WinMagDev.LBCompas.ListCount-1, 1) = oneCell
  Next
Wend

Column count = 24

is done.
but still same situation.

LBCompas open event

For i = 0 to 360 step 15
//LBCompas.AddRow(format(i,“000”)+“°”) //disabled
Next

here Mag.txt with 24 value
are loaded to LBCompas as shown on this screenshot, off course the first column is empty, that’s a problem.
listBx2

Are-you sure it is LastIndex that returns the last added Row ?
(I always have doubts…)

Try LastIndex Instead:

    WinMagDev.LBCompas.Cell(WinMagDev.LBCompas.LastIndex, 1) = oneCell

Emile i have already done.

This is listbox open event.

dim i as integer

for i=0 to 1
LBCompas.ColumnAlignment(i)=LBCompas.AlignCenter
next

Me.columncount=2
Me.columnwidths=“45%,55%”
Me.HasHeading=True
Me.heading(0)=“CC”
Me.heading(1)=" dev "

For i = 0 to 360 step 15
LBCompas.AddRow(format(i,“000”)+“°”)
Next

LBCompas.ColumnType(1)=LBCompas.TypeEditable
LBCompas.ColumnAlignment(1)=LBCompas.AlignRight

Is it correct ?

Yes, but you place nothing in Column(1)

And you set the Alignment for Column(1) as center at the top and right at the bottom.

This is wrong:
LBCompas.AlignRight

Write:
ListBox.AlignRight

Code in a button:

  Dim i As Integer
  Dim Loc_Row As Integer

LBCompas.ColumnType(1)=ListBox.TypeEditable
LBCompas.ColumnAlignment(0) = ListBox.AlignCenter
LBCompas.ColumnAlignment(1)=ListBox.AlignRight

  LBCompas.columncount=2
  LBCompas.columnwidths="45%,55%"
  LBCompas.HasHeading=True
  LBCompas.heading(0)="CC"
  LBCompas.heading(1)=" dev "
  
  For i = 0 to 360 step 15
    LBCompas.AddRow(format(i,"000")+"°")
    Loc_Row = LBCompas.LastIndex
    LBCompas.Cell(Loc_Row, 1) = "Emile"
  Next

listbox with your code Emile
ListboxEmile

When “Emile” line is under comment the first column remain empty.

I discover in data Mag.txt file a blank in the first line .
when blank deleted a file is read from the second line, first line is omitted .
Really Distraught !
Data file

Time to eat something, drink a coffe and get back to Xojo.

Works fine with me when face a big trouble.

Emile i will use an alternative solution, one list box with one column as (labels) .
one list box with one column for data.
Sorry Emile, i have enough abused your time.

Not time abuse today. Happy to help.

Vision bad today.

I am downloading Big Sug Update (>6GB)… so all I can do is wait…