A list with columns
A ListView in details view shows rows with columns, check boxes and a header that can be clicked. This lesson builds a task list, sorts it by clicking a column header, counts the checked rows and removes them, keeping the data in a Ring list.
What you will learn#
ListViewin the details view, withColumns,- rows (
ListViewItem) and their cells (SubItems), FullRowSelect,GridLinesandCheckBoxes,- the event of a click on a column header and its arguments,
- sorting: the data is a Ring list, the view only shows it,
- comparing text with
strcmp, - removing the checked rows.
The tutorial A folder browser filled a ListView with files. This lesson makes the list work: sorting, checking and removing.
The program#
# a list with columns - ListView in details view: columns, check boxes, sorting by a column header, removing the checked rows
load "dotnet.ring"
using {
"System.Drawing"
"System.Windows.Forms"
}
# controls and data that the event code uses are global variables
oForm = ""
oList = ""
oStatus = ""
aTasks = [ ["Write the report",2,"2030-03-14"], ["Call the bank",1,"2030-03-11"], ["Buy tea",3,"2030-03-12"], ["Book the room",1,"2030-03-20"], ["Back up the files",2,"2030-03-09"] ]
nSortColumn = -1 # no column yet
lAscending = True
func main
oForm = uiForm("Tasks",520,330)
oList = new ListView()
oList.Location = new Point(15,15)
oList.Size = new Size(485,220)
oList.View = "Details" # columns with titles (the other views: List, SmallIcon, LargeIcon, Tile)
oList.FullRowSelect = True # a click selects the whole row, not only the first cell
oList.GridLines = True
oList.CheckBoxes = True # a check box in front of each row
oList.Columns.Add([ :text = "Task", :width = 220 ])
oList.Columns.Add([ :text = "Priority", :width = 80 ])
oList.Columns.Add([ :text = "Due", :width = 120 ])
# a click on a header: the event arguments (aDotNetArgs[2]) have the number of the column
oList.setColumnClickEvent("sortBy(aDotNetArgs[2].Column)")
oList.setItemCheckedEvent("showStatus()")
oForm.Controls.Add(oList)
uiButton(oForm,"Remove checked",15,248,140,30,"removeChecked()")
oStatus = uiLabel(oForm,"",170,254,330,22)
fillList()
uiRun(oForm)
# the rows come from the Ring list aTasks: the list is the DATA and the ListView only shows it, so sorting means sorting the list
# and filling the view again
func fillList
oList.Items.Clear()
for aTask in aTasks
oItem = new ListViewItem().@New([ :text = aTask[1] ])
oItem.SubItems.Add("" + aTask[2])
oItem.SubItems.Add(aTask[3])
oList.Items.Add(oItem)
next
showStatus()
# sort by a column: the same column again turns the order around. A sort with a Ring function: a plain bubble sort on the column.
func sortBy nColumn
if nColumn = nSortColumn
lAscending = not lAscending
else
nSortColumn = nColumn
lAscending = True
ok
nCount = len(aTasks)
for i = 1 to nCount - 1
for j = 1 to nCount - i
if goesAfter(aTasks[j],aTasks[j + 1])
aTemp = aTasks[j]
aTasks[j] = aTasks[j + 1]
aTasks[j + 1] = aTemp
ok
next
next
fillList()
# does the first row go after the second? The cells are compared as TEXT with strcmp (it gives a number: less than 0, 0 or more than 0),
# because the signs < and > compare numbers. Dates written as year-month-day sort correctly as text; the priority is a number, so it is
# written with the same number of digits ("2") and compares the same way.
func goesAfter aFirst,aSecond
nColumn = nSortColumn + 1
nResult = strcmp("" + aFirst[nColumn],"" + aSecond[nColumn])
if lAscending
return nResult > 0
ok
return nResult < 0
func removeChecked
aKept = []
for i = 1 to len(aTasks)
if not oList.Items.Item(i - 1).Checked
aKept + aTasks[i]
ok
next
aTasks = aKept
fillList()
# the number of checked rows: ask each row (the property Checked)
func checkedCount
nChecked = 0
for i = 0 to oList.Items.Count - 1
if oList.Items.Item(i).Checked
nChecked++
ok
next
return nChecked
func showStatus
oStatus.Text = "" + oList.Items.Count + " tasks, " + checkedCount() + " checked"
How it works#
Columns and rows#
View = "Details" shows the columns. Columns.Add([ :text = "Task", :width = 220 ]) adds a column with a title. A row is new ListViewItem().@New([ :text = cName ]): its text is the first cell, and SubItems.Add(text) adds the others. Items.Add(item) puts the row into the list. Items.Item(0) is the first row (counted from 0) and SubItems.Item(2) its third cell. CheckBoxes = True puts a check box in front of each row, and Checked is true when it is on.
The data is the list#
The program keeps the tasks in a Ring list aTasks, and fillList clears the ListView and creates the rows again. Sorting then means sorting the list and filling the view again; removing a row means removing the item from the list. The view never has information that the list does not have, so the two cannot disagree.
The click on a header#
setColumnClickEvent("sortBy(aDotNetArgs[2].Column)") runs the code when the user clicks a header. The variable aDotNetArgs is [sender, eventArgs] and the event arguments of this event have the number of the column. The same column again turns the order around (lAscending); another column starts with the ascending order. The test calls sortBy itself, because a test cannot click a header.
Comparing text#
The signs < and > compare numbers in Ring; with text they raise an error. strcmp(a, b) compares two texts and gives a number: less than 0 when a is first, 0 when they are the same, more than 0 when a is later. The function goesAfter joins each cell with "" first, so numbers (the priority) and text (the name, the date) use the same code. Dates written as year-month-day sort correctly as text, which is one of the reasons for this form.
This is a simple bubble sort, enough for a list of a few hundred rows. For a big list sort with sort() of Ring (sort(aList, 3) sorts a list of lists by the third item) or use the DataGridView of the tutorial Layout and richer controls.
Checked rows#
setItemCheckedEvent runs when a check box changes; the code shows the number of tasks and of checked rows in the status label. removeChecked goes through the rows, keeps the tasks of the rows that are not checked and fills the view again.
What you learned#
- the details view with columns, cells and check boxes,
- the header click event and
aDotNetArgs, - a Ring list as the data and a view that only shows it,
- sorting with
strcmp, - removing the checked rows.
Exercises#
- Show a small arrow in the title of the sorted column (
oList.Columns.Item(0).Text = "Task ▲"). - Add a text box that filters the rows: only tasks that contain the typed text are shown (keep the full list in
aTasksand fill the view from a filtered copy). - Show a message with the selected task when the user double-clicks a row (the event
DoubleClickandSelectedItems). - Use
sort(aTasks, 2)of Ring for the sort by priority and compare the code withsortBy.