Converting from listbox into listbox and textfield

Hi,

I want to view rows from a database which is populated into a listbox.

I need the listbox(Listbox Source) to be shown on second listbox(listbox Destination) and textfield.

Window1.Listbox1.DeleteAllRows
Window1.L_guest.text=Listbox1.cell(Listbox1.ListIndex,1)
Window1.l_hp.text=Listbox1.cell(Listbox1.ListIndex,2)
dim ct As integer = 0

Window1.Listbox1.AddRow ""
dim h as integer
for h=0 to Listbox1.ListCount-1
Window1.listBox1.cell(ct,1)=listBox1.cell(h,5)
Window1.listBox1.cell(ct,2)=listBox1.cell(h,6)
Window1.listBox1.cell(ct,3)=listBox1.cell(h,7)
Window1.listBox1.cell(ct,4)=listBox1.cell(h,8)
Window1.listBox1.cell(ct,5)=listBox1.cell(h,9)

next
ct = ct + 1

Textfield showing correct result, but the listbox only show the last row from source listbox (listbox source)

any helps ?

thanks
arief

Move that line before next (into the loop.

Also, using: Window1.listBox1 and listBox1 is confusing: where is located that line of code ?
If it is somewhere inside Window1, you are in troubles… both references goes to the same ListBox Control.

I did, but it still show an outbound exception.

The Source listbox place in Window2 and the destination listbox is on window1

The code above is execute from a pushbutton1.

thanks
arief

listBox1.cell(h,9)

Do you have 10 columns in the source ListBox ?

IS pushbutton1 located in Window2 ?

This emptied out your Window1.Listbox1

If you move ct = ct + 1 inside the loop, you are trying to access multiple rows that don’t exist as you only added one blank row with

Window1.Listbox1.AddRow ""

You need to add some rows back into Window1.Listbox1

hi,
Yes, its 10 Columns on source listbox, and also pushbutton is on window2.

I am using other method to save as file, its worked normally.

dim TextOut as textoutputstream
dim f as new folderitem
f=getfolderitem("data").Child("test.open")
TextOut = f.CreateTextFile
TextOut.Writeline Listbox1.cell(listbox1.ListIndex,0)
TextOut.Writeline Listbox1.cell(listbox1.ListIndex,1)
TextOut.Writeline Listbox1.cell(listbox1.ListIndex,2)
TextOut.Writeline Listbox1.cell(listbox1.ListIndex,3)
TextOut.Writeline Listbox1.cell(listbox1.ListIndex,4)

dim i as integer
for i=0 to listBox1.listcount-1
  TextOut.writeline listBox1.cell(i,5)
  TextOut.writeline listBox1.cell(i,6)
  TextOut.writeline listBox1.cell(i,7)
  TextOut.writeline listBox1.cell(i,8)
  TextOut.writeline listBox1.cell(i,9)
next
TextOut.Close

the error still the same an outbound exception.

thanks
arief

Out Of Bounds Exception means you try to access to a Row (Column) that do not exists.

So, add a breakpoint in the For line and in the debugger look at what line have the Out Of Bounds Exception.

Have-you checked Julian advices ?

Yes,
I do and I have modified it, then its worked now.

Dim numrows, numcols As Integer
Dim r, c As Integer
numrows = listbox1.listcount - 1

Window1.Listbox1.DeleteAllRows
Window1.L_guest.text=Listbox1.cell(Listbox1.ListIndex,1)
Window1.l_hp.text=Listbox1.cell(Listbox1.ListIndex,2)



For r = 0 to numrows
  Window1.Listbox1.AddRow ""
  For c = 0 to numcols
    Window1.listbox1.cell(r, 1) = listbox1.cell(r, 5)
    Window1.listbox1.cell(r, 2) = listbox1.cell(r, 6)
    Window1.listbox1.cell(r, 3) = listbox1.cell(r, 7)
    Window1.listbox1.cell(r, 4) = listbox1.cell(r, 8)
    Window1.listbox1.cell(r, 6) = listbox1.cell(r, 9)
  Next c
Next r

thanks for al the helps.

regards,
arief

I’m glad you got it sorted.

By the way, the For c does nothing, so you could change this:

to this:

For r = 0 To numrows
  Window1.Listbox1.AddRow "", _
  listbox1.cell(r, 5), _
  listbox1.cell(r, 6), _
  listbox1.cell(r, 7), _
  listbox1.cell(r, 8), _
  listbox1.cell(r, 9)
Next r

to make things a little neater, this adds a row and uses the values from the other listbox at the same time so you don’t have to add a blank row then populate it.

1 Like