Listbox and textfields

Hey all,

What would be the best way to have a selected row on a listbox fill several textfields with information?

I was using “If” statements In the listbox change event. Like so…

    If listbox1.selected(0) then
    Textfield1.text = "Mytext"
    Textfield2.text = "Mytext"
    Textfield3.text = "Mytext"
    Textfield4.text = "Mytext"
    Textfield5.text = "Mytext"
    End if

If listbox1.selected(1) then
    Textfield1.text = "Mytext"
    Textfield2.text = "Mytext"
    Textfield3.text = "Mytext"
    Textfield4.text = "Mytext"
    Textfield5.text = "Mytext"
    End if

And so on…

Is this the best method for this? As I have alot of Listbox rows.

No If … necessary:

Textfield1.text = listbox1.cellValueAt( listbox1.selectedRowIndex, 0 )
Textfield1.text = listbox1.cellValueAt( listbox1.selectedRowIndex, 1 )
// … etc

or somewhat easier to type:

tf.text = lb.cellValueAt( lb.selectedRowIndex, 0 )
tf.text = lb.cellValueAt( lb.selectedRowIndex, 1 )
// … etc

And to save to the listbox just swap the left and right side of the equation.

Note that that way you use the Listbox for both display of data and data storage.

Play around with and get comfortable with the ListBox, and when you are ready I recommend you use a more object-oriented approach with classes. Just open another thread then.

1 Like

You could put a class instance in the rowtag for each row, with the class having as a property an array with as many elements as you have textfields. Assuming your class instances are populated with the text strings, it’s easy enough to do this in n or so statements, where n is the number of your textfields.

1 Like

Or you use the -1 trick and do it in a single statement …

oh! nice. That is so much simpler. Thank you Markus!

Hi Tim, I will need to read over the language ref on classes and such. I’m still fairly new to Xojo.

I have another question. I have 2 Listboxes in my project. Listbox1 has several rows, When one row is selected it occupies a few rows on Listbox2. But when I keep selecting the row on Listbox1 it is adding more rows in listbox2. How do I prevent this?

Not sure I really understand, but delete first the previous Rows from ListBox2 ?
So, you always get only a set of Row (same number of Rows).

Hi Emile, Apologies.

In Listbox1 I have a row that says “Click Here”. In the Listbox1 change event. I have an “if” statement…

If Listbox1.selected(0) Then
Listbox2.addrow ("Test1")
end if

This does work like it should. The problem I have is when I keep selecting the row in listbox1 it keeps adding “Test1” to Listbox2. I just want it to add the row to listbox2 once.

So, delete all rows in ListBox2 in ListBox1.Change…

1 Like

I’m not too sure if I understand now lol, I’m so very sorry Emile.

As you can see in the screenshot below, Every time i select “Click Me” in listbox1. It keeps adding “Test1” to listbox2. I just need it to add “Test1” only once, not every time I click “Click Me”.

I would like to retain the “Click Me” row in LB1.

XojoForums

Apologies again Emile!

My brain just clicked and its working lol think I was looking at the screen for too long. It was really pickling me.

Thank you!

Now, you may need to place the data into a TextField, instead of into another ListBox.