ListView Control - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

ListView Control

Description:

Subitems are collections of strings representing the ListItem object's data that ... the click, double click any row in the designer, and fill in the skeleton code: ... – PowerPoint PPT presentation

Number of Views:391
Avg rating:3.0/5.0
Slides: 12
Provided by: edwardj7
Category:
Tags: listview | control

less

Transcript and Presenter's Notes

Title: ListView Control


1
ListView Control
  •              
  • The ListView control displays items using one of
    four different views. You can arrange items into
    columns with or without column headings as well
    as display accompanying icons and text.
  • With a ListView control, you can organize list
    entries, called ListItem objects, into one of
    four different views
  • Large (standard) Icons
  • Small Icons
  • List
  • Details
  • The View property determines which view the
    control uses to display the items in the list.
    This project will use Details only. You can also
    control whether the labels associated with items
    in the list wrap to more than one line using the
    LabelWrap property. In addition, you can manage
    how items in the list are sorted and how selected
    items appear.
  • Add a ListView control to the form by clicking on
    the ListView Icon in the toolbox and placing it
    on the form in the usual method.

2
ListView Objects
The ListView control is an array of ListItems.
The number of rows is the number of ListItems.
The number of columns is the number of
ColumnHeader objects in the ListView. The number
of columns in the ListView also determines the
number of subitems in each ListItem. A ListItem
object defines the various characteristics of
items in the ListView control, such as A brief
description of the item. Icons that may appear
with the item, supplied by an ImageList
control. Additional pieces of text, called
subitems, associated with a ListItem object that
you can display in Report view.
3
Adding Headers
  • Headers can be added at both design and run time.
  • At design time, you can use the Columns tab of
    the ListView Control Properties dialog box. Open
    the ListView Properties dialog box by clicking on
    the ListView. Click the show dialog icon, which
    will open the ColumnHeader Collection Editor
    Dialog Box.
  • Each time you click Add, you will get another box
    to fill in with the properties of the column
    header.
  • You can choose to display or hide column headings
    in the ListView control using the HeaderStyle
    property. None will hide the headers,
    Nonclickable will show them, but clicking will
    have no effect, and Clickable will allow a column
    to be selected by clicking on it. More about that
    selection later. We will make ours Clickable.

4
ListViewItem
  • The Items property of a ListView is a collection
    of ListViewItems.
  • A ListViewItem has many properties. It is the
    ListViewItem that will fill one row of the
    ListView. The first column is the Text property.
    The remaining columns are filled with a
    collection called SubItems.
  • Subitems are collections of strings representing
    the ListItem object's data that are displayed in
    Report view. For example, you could show the file
    size and the date last modified for a file.
  • A ListItem object can have any number of
    associated item data strings (subitems) but each
    ListItem object must have the same number of
    subitems.
  • There are corresponding column headers defined
    for each subitem.
  • The Add method is used to enter data into the
    SubItems from first to last. They are being put
    into a collection that is the same size as the
    number of columns.

5
Adding a ListItem
// This is executed when the Keep button is
clicked // The ListView has been filled from a
file (or is newly created) private void
cmdAdd_Click(object sender, System.EventArgs
e) //First create a new ListViewItem Lis
tViewItem newItem new ListViewItem() //The
n fill in the values, starting with
Text //then Adding the rest newItem.Text
txtName.Text newItem.SubItems.Add(txtId.Text)
newItem.SubItems.Add(txtMajor.Text) //
Finally, Add the ListViewItem to the Items
property lvwRoster.Items.Add(newItem)
6
Sorting the Items
The listview has a property that determines if
the items are sorted. Sorting can be set to None,
Ascending or Descending. All but None cause the
listview items (the rows) to be sorted. If we do
nothing else but set Sorting to a value that
sorts, the items are sorted by the first column
(the Text property of each ListViewItem). Chosing
the column to use for sorting The first
necessity for sorting by columns is to set the
HeaderStyle property to Clickable. Next we set up
a listener for the column header clicks //
Connect the ListView.ColumnClick event to the
ColumnClick event handler. this.lvwRoster.ColumnCl
ick new ColumnClickEventHandler(Co
lumnClick) This goes in the hidden
InitializeComponent() method. It seems logical
that this might also be added by double clicking
on a column header, but this doesnt work.
7
Sorting Continued
Next we need to add and complete the column click
handler, which is partially filled in by double
clicking the column header // ColumnClick event
handler. private void ColumnClick(object o,
ColumnClickEventArgs e) // Set the
ListViewItemSorter property to a new
ListViewItemComparer // object. Setting this
property immediately sorts the // ListView
using the ListViewItemComparer object. this.lvwRo
ster.ListViewItemSorter new
ListViewItemComparer(e.Column) this.lvwRost
er.Sort() We have to add the
ListViewItemComparer that appears above, with the
code on the next slide
8
Sorting Continued 2
// Implements the manual sorting of items by
columns. class ListViewItemComparer IComparer
private int col public ListViewItemComparer()
col 0 public ListViewItemComparer(int
column) col column public int
Compare(object x, object y) if(col 1)
return ( (int)(Double.Parse(((ListV
iewItem)x).SubItemscol.Text) -
Double.Parse(((ListViewItem)y).SubItemscol.Text)
) else return String.Compare(((ListViewIt
em)x).SubItemscol.Text,
((ListViewItem)y).SubItemscol.Text)
The handler and the sort method use these methods.
9
Accessing an Item
  • To access a ListItem, click on the row.
  • To create the handler for the selection chosen by
    the click, double click any row in the designer,
    and fill in the skeleton code
  • private void lvwRoster_SelectedIndexChanged(object
    sender, System.EventArgs e)
  • if(lvwRoster.SelectedIndices.Countgt0)
  • ListViewItem item lvwRoster.SelectedItems0
  • txtName.Text item.Text
  • txtId.Text item.SubItems1.Text
  • txtMajor.Text item.SubItems2.Text
  • The if is needed because this event is raised
    twice when a row is clicked, first when the
    previous selection is removed (count 0) and
    once when the new selection is recorded (count
    1). This prevents an error on the first event.

10
Initializing a ListView from a File
The following code initializes from a file chosen
by a Dialog Box. private void frmRoster_Load(objec
t sender, System.EventArgs e) try ofdOpenRoster.
ShowDialog() string fileName
ofdOpenRoster.FileName loadedFile
fileName StreamReader rosterStreamReader
new StreamReader(fileName) while(rosterStre
amReader.Peek() ! -1) string nextLine
rosterStreamReader.ReadLine() string split
nextLine.Split(',') ListViewItem newItem
new ListViewItem() newItem.Text
split0 newItem.SubItems.Add(split1) newI
tem.SubItems.Add(split2) lvwRoster.Items.Add(
newItem) catch(Exception e1)MessageBox.Show(
e1.Message)
11
Saving a ListView
The following code saves the contents of
lvwRoster in a file specified by a Dialog
Box. private void cmdSave_Click(object sender,
System.EventArgs e) try sfdSaveRoster.FileName
loadedFile sfdSaveRoster.ShowDialog() Strea
mWriter rosterWriter new StreamWriter(sfdSave
Roster.FileName) for(int i 0 i lt
lvwRoster.Items.Count i) ListViewItem item
lvwRoster.Itemsi String line
item.Text line "," item.SubItems1.Text
line "," item.SubItems2.Text rosterWri
ter.WriteLine(line) rosterWriter.Close()
catch(Exception e1)MessageBox.Show(e1.Message)
Write a Comment
User Comments (0)
About PowerShow.com