Adding Rows/Columns to a listbox.

Hi everyone,

I’m practicing some with the listbox and I’m trying to figure out how to add multiple bits of information from text fields (inputs from the user) into the listbox. So far I know how to add two columns worth of information, but I’m stuck on how to add 3 or more columns for one row. This is the code I know so far:

Listbox1.AddRow(rs.Field("firstname").StringValue) Listbox1.Cell(Listbox1.LastIndex,1) = rs.Field("lastname").StringValue

I’ve also adjusted the number of columns in the inspector. I would like to add other columns for more information, but I’m not sure how to do that. Is there a general Xojo code structure to reference regarding this? I searched the Xojo documents, but I only say literature for Adding Rows.

Kayla,
look in the latest project file I sent you, (I’m not sure if you downloaded the very latest) and then just expand on the example given.

Alternatively, send me the project file, with a description of exactly what you need to do - and I will fix it, and comment fully what I did.

Hope that helps.

You can also add several columns at once:

Listbox1.ColumnCount = 3 Listbox1.AddRow( _ rs.Field("firstname").StringValue, _ rs.Field("lastname").StringValue, _ rs.Field("city").StringValue _ )

If retrieving the info from a database (as your example code suggests), it’s basically a case of adding the new column name:
A) when you retrieve the RecordSet results, and B) when you add the row to the ListBox.

Obviously, you will also need to do the same thing (add the new column name) when adding new entries to the database (as opposed to retrieving them) - otherwise you will be trying to retrieve something which doesn’t yet exist :slight_smile:

@Richard Summers and @Eli Ott

Thanks for the replies!

I added the code that Eli suggested, but now I’m getting this error message:

For reference, here’s the link to the file. My Xojo Practice File

Kayla,
See this part of my post above:

I added this code to the Add button, but still the same issue happens.

Dim rec As DatabaseRecord rec = New DatabaseRecord rec.Column("id") = supidfield.Text rec.Column("company") = SupCompField.Text rec.Column("city") = cityfield.Text MyDatabase.InsertRecord("supplierinfo", rec) If MyDatabase.Error Then MsgBox MyDatabase.ErrorMessage Else MyDatabase.Commit Populate End If

You are trying to insert “suppplierinfo” into a column which doesn’t exist?

See the Private Message I just sent you.

“city” was an example only. You need to replace it with a column name of your table.

you use

MyDBFile = SpecialFolder.ApplicationData.Child("Ch17")

Ch17 is a file without extension

better use

dim suppFolder as FolderItem = SpecialFolder.ApplicationData.Child(App.ExecutableFile.Name) if not suppFolder.Exists then suppFolder.CreateAsFolder MyDBFile = suppFolder.Child("Ch17.sqlite")

From the Language Reference on Recordset (2nd example)

Generic means of filling a listbox from a recordset. Not necessarily what you want but a good method to study.

[code]Sub PopulateListBox(dataList As Listbox, rs As RecordSet)
If rs Is Nil Then Return

// set up listbox state for population
dataList.DeleteAllRows
dataList.Columncount = rs.Fieldcount

// Add the DB columns as the headers for the ListBox
dataList.ColumnCount = rs.FieldCount
dataList.Column(-1).WidthExpression = “100”
For i As Integer = 0 To rs.FieldCount-1
dataList.Heading(i) = rs.IdxField(i+1).Name
Next

// Add the data from the table
While Not rs.EOF
dataList.AddRow("")

For i As Integer = 0 To rs.FieldCount-1
  dataList.Cell(dataList.LastIndex, i) = rs.IdxField(i+1).StringValue
Next

rs.MoveNext

Wend
End Sub[/code]