### Customized ListView with Type Data

This is an example of a Customized Listview showing in two lines the content of a sorted typed list.
The layout file contains a ListView and a Label
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
'The type used
Type ContactType (Name As String, EMail As String, ID As Int)
'The list holding the contacts
Private ContactList As List
'Show the content of a list entry
Private lblContact As Label
'The customized listview displaying the contact entries in two lines
Private lvContacts As ListView
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
MainForm.SetFormStyle("UTILITY")
MainForm.Show
CreateContactList
ListContactList
FillListView
End Sub`
'Create a sorted typed list. Sorted by field Name
'Type ContactType (Name As String, EMail As String, ID As Int)
`Sub CreateContactList
ContactList.Initialize
For i=0 To 4
Dim ct As ContactType
Dim name As String = RandomName(4)
ct.Name = $"${name}"$
ct.EMail = $"${name}@email.adresse"$
ct.ID = i
ContactList.Add(ct)
Next
ContactList.SortType("Name", True)
End Sub`
`Sub ListContactList
For Each ct As ContactType In ContactList
Log($"${ct.Name},${ct.EMail},${ct.ID}"$)
Next
End Sub`
`Sub FillListView
For Each ct As ContactType In ContactList
AddTwoLines2(lvContacts, ct.Name, ct.EMail, ct.ID)
Next
If lvContacts.Items.Size > 0 Then lvContacts.SelectedIndex = 0
End Sub`
'Close the app - add any specifics here
`Sub AppClose
MainForm.Close
End Sub`
'Handle form closing via system close (top right X button)
`Sub MainForm_CloseRequest (EventData As Event)
AppClose
End Sub`
Region LISTVIEW
'Get the entry from the contactlist using the listviewindex
`Sub lvContacts_SelectedIndexChanged(Index As Int)
If Index < 0 Then Return
Dim ct As ContactType = ContactList.Get(Index)
lblContact.Text = $"Name:${CRLF}${ct.Name}${CRLF}${CRLF}EMail:${CRLF}${ct.EMail}"$
End Sub`
'NOTE: In case the data wants to be retrieved from the listview, use like
If Index > = -1 Then
Dim ap As AnchorPane = lvContacts.SelectedItem
Dim lbl1 As Label = ap.GetNode(0)
Dim value As Object = lbl1.Tag
Log(value)
...
End If
`Sub AddTwoLines2(lv As ListView, Line1 As String, Line2 As String, Value As Object)
Dim ap As AnchorPane
ap.Initialize("")
Dim lbl1, lbl2 As Label
lbl1.Initialize("")
lbl1.Text = Line1
lbl1.Font = fx.DefaultFont(16)
lbl2.Initialize("")
lbl2.Text = Line2
lbl2.Font = fx.DefaultFont(12)
ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
ap.AddNode(lbl2, 0, 25dip, lv.Width, 20dip)
lv.Items.Add(ap)
lbl1.Tag = Value
End Sub`
End Region
`Sub RandomName(l As Int) As String
Dim sb As StringBuilder
sb.Initialize
For i = 0 To 3
sb.Append(Chr(Rnd(65, 90)))
Next
Return sb.ToString
End Sub`

Changed: , Category: ListView, File: T6C216M1588.htm, ID: 1588