### Create Context Menu

To add a context menu to a ListView, following steps when using the B4J Visual Designer.
B4J Visual Designer
After adding a View ListView, select the Control Property Context Menu.
Create the Menu. For this example 3 Menu Items:
[
{Text:"Add", Tag:"cmnuAdd", EventName: "cmnuAdd"},
"-",
{Text:"Delete", Tag:"cmnuDelete", EventName: "cmnuDelete"},
"-",
{Text:"About", Tag:"cmnuAbout", EventName: "cmnuAbout"}
]
B4J Code
The events as defined in the Context Menu are handled by Subs EventName_Action
'Add a new item to the listview
`Sub cmnuAdd_Action
lvwMain.Items.Add(Rnd(0, 100))
End Sub`
'Delete the selected item
`Sub cmnuDelete_Action
If lvwMain.SelectedIndex < 0 Then Return
lvwMain.Items.RemoveAt(lvwMain.SelectedIndex)
End Sub`
'Show about dialog
`Sub cmnuAbout_Action
fx.Msgbox(MainForm, "B4J HowTo ListView Context Menu", "About")
End Sub`
B4J Full Code Example
The layoutfile contains Views ListView and Label (used to show the selected ListView item).
`Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private lvwMain As ListView
Private lblSelected As Label
End Sub`
`Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("main")
MainForm.Show
For i = 1 To 5
lvwMain.Items.Add(i)
Next
lvwMain.SelectedIndex = 0
lvwMain.RequestFocus
End Sub`
`Sub lvwMain_SelectedIndexChanged(Index As Int)
If Index < 0 Then Return
lblSelected.Text = lvwMain.SelectedItem
End Sub`
`Sub cmnuAdd_Action
lvwMain.Items.Add(Rnd(0, 100))
End Sub`
`Sub cmnuDelete_Action
If lvwMain.SelectedIndex < 0 Then Return
lvwMain.Items.RemoveAt(lvwMain.SelectedIndex)
End Sub`
`Sub cmnuAbout_Action
fx.Msgbox(MainForm, "B4J HowTo ListView Context Menu", "About")
End Sub`

Changed: , Category: ListView, File: T6C216M1578.htm, ID: 1578