### Customized Listview with Buttons

To create a ListView with Buttons, the Standard ListView has to be customized.
Below example creates a ListView with 3 Buttons and related Action events.
Usage of this solution, could be a dynamic Menu in a Form or as a Popup Menu.
Define the ListView as a Global
`Sub Process_Globals
...
Dim lvwMenu As ListView
...
End Sub`
Custom the ListView with Button controls
Added are 3 Button controls to function as a Menu.
' Build the ListView with Buttons functioning as Menubuttons
' Add the string functions as buttons - these will be executed via Sub function_Action, like MenuItemA_Action
' To add functions: add entry below and add sub function_Action
' Example: addListViewButton("MenuItemB"); Sub MenuItemB_Action
' For this case, a list is used to sort the Menu Items
`Sub buildListViewMenu
Dim l As List
l.Initialize
l.AddAll(Array As String("MenuItemA", "MenuItemB", "MenuItemC"))
l.Sort(True)
For i = 0 To l.Size - 1
addListViewButton(l.Get(i))
Next
End Sub`
' Add a button to the ListView. The eventname is the same as the button text.
' Any Button property can be set
`Sub addListViewButton(s As String)
Dim b As Button
b.Initialize(s)
b.PrefWidth = 150
b.Id = s
b.Text = s
lvwMenu.Items.Add(b)
End Sub`
Define the Button actions
`Sub MenuItemA_Action
Dim b As Button = Sender
txtInformation.Text = "You have selected Menu Button " & b.Text
End Sub`
`Sub MenuItemB_Action
Dim b As Button = Sender
txtInformation.Text = "You have selected Menu Button " & b.Text
End Sub`
`Sub MenuItemC_Action
Dim b As Button = Sender
txtInformation.Text = "You have selected Menu Button " & b.Text
End Sub`
As previous but Buttons created from a Map with customized Button Text
`Sub buildListViewMenuMap
Dim m As Map = CreateMap("MenuItemA": "Menu A", "MenuItemB": "Menu B", "MenuItemC": "Menu C")
For i = 0 To m.Size - 1
addListViewButtonMap(m.GetKeyAt(i), m.GetValueAt(i))
Next
End Sub`
' Add a button to the ListView. the button text differs from the eventname
' Any Button property can be set
`Sub addListViewButtonMap(id As String, text As String)
Dim b As Button
b.Initialize(id)
b.PrefWidth = 150
b.id = id
b.text = text
lvwMenu.Items.Add(b)
End Sub`
As previous but Listview show selected row
To show the selected row or the button clicked, use the Tag property
' Add a button executing a string function to the listview
`Sub addListViewButton(tag As Int, s As String)
Dim b As Button
b.Initialize(s)
b.PrefWidth = 150
b.Id = s
b.Text = s
b.tag = tag
lvwMenu.Items.Add(b)
End Sub`
`Sub buildListViewMenu
Dim l As List
l.Initialize
l.AddAll(Array As String("MenuItemA", "MenuItemB", "MenuItemC"))
l.Sort(True)
For i = 0 To l.Size - 1
addListViewButton(i, l.Get(i))
Next
End Sub`
`Sub MenuItemA_Action
Dim b As Button = Sender
lvwMenu.SelectedIndex = b.Tag
txtInformation.Text = "You have selected Menu Button " & b.Text
End Sub`
...
Notes
• The Button actions remain as previous described.
• More to find under Various Projects Examples.

Changed: , Category: ListView, File: T6C216M1582.htm, ID: 1582