[For those who noted that the above graphic lacks one of the fundamental requirements for an animation (namely animated-ness), CodeProject doesn't support animations within a page, so click here to see the actual animation.]
All projects suffer creeping featurism. Things that start simple and elegant end up as the "before" image in a weight-loss ad. This control has grown considerably since it was first written. If you want to do something with a ListView, this control probably has some code to help you do it. For those in a hurry, this control has the following major features:
ListView
TreeListView
VirtualObjectListView
FastObjectListView
DataListView
FastDataListView
IVirtualListDataSource
This control has its own website, hosted by SourceForge: ObjectListView - How I Learned To Stop Worrying and Love .NET's ListView (made using the cool Sphinx documentation tool). This is not an empty shell site. It actually has lots of useful information. There you can find a step by step tutorial to help you get started, as well as a cookbook showing you how to accomplish common tasks. This article is only really an introduction.
Those who aren't in a hurry can now read the rest of the article. " />
Larry Wall, the author of Perl, once wrote that the three essential character flaws of any good programmer were sloth, impatience and hubris. Good programmers want to do the minimum amount of work (sloth). They want their programs to run quickly (impatience). They take inordinate pride in what they have written (hubris). It is in the interests of encouraging "slothfulness" that I wrote this control.
I often find that I have a collection of objects which I want to present to the user in some sort of tabular format. It could be the list of clients for a business, a list of known FTP servers or even something as mundane as a list of files in a directory. User interface-wise, the ListView is the perfect control for these situations. However, I find myself groaning at the thought of using the ListView and secretly hoping that I can use a ListBox instead.
ListBox
The reason for wanting to avoid the ListView is all the boilerplate code it needs to work: make the ListViewItems, add all the SubItems, catch the header click events and sort the items depending on the data type. Each of these tasks is slightly different for each instance of a ListView. If you want to support grouping, there's an even bigger chunk of boilerplate code to copy and then modify slightly.
ListViewItems
SubItems
For a basically lazy person, this is far too much work. ObjectListView was born to relieve this workload.
ObjectListView
An ObjectListView provides two groups of functionality. The first group is designed to make a ListView much easier to use. This group ranges from automatically converting a list of model objects into a fully functional ListView, to making drag and drop and cell editing much easier to use. The second group adds new features to a ListView, such as image overlays and customisable tooltips.
ListView,
There are two ways to use an ObjectListView in your project:
Once your project has been built, there should now be a new section in your Toolbox, "ObjectListView Components". In that section should be ObjectListView and its friends. You can then drag an ObjectListView onto your window, and use it as you would a standard ListView control.
If you don't want to add the ObjectListView project to your project, you can also just add the ObjectListView.dll file.
Adding the DLL does not automatically add any new components into your toolbox. You will need to add them manually after you have added the DLL to your project.
"Simple things should be simple. Complex things should be possible."
The major design goal of ObjectListView is to make the programmer's life simpler. However, this control provides a lot of functionality, and it can be overwhelming if you try to absorb it all at once. It's best to start with the foundational ability of an ObjectListView: it generates a fully functional ListView from a list of model objects. Usually the control is configured within the IDE (setting properties and creating columns) and then, with a single line of code, it is put into action like this:
this.objectListView1.SetObjects(allPeople);
And that is it!
Simple, fast, uncomplicated, non-fattening and without a single line of boilerplate code. Without further work, this ObjectListView is a fully functional ListView and will handle drag and drop, alternate line colouring, column-click sorting, data formatting, grouping and possibly editing too. The "Simple Example" tab in the demo project shows what is possible with only IDE configuration and this one line of code.
What is actually happening here? When you call SetObjects(), the ObjectListView runs through the given list of model objects, extracts the aspect nominated for each column, converts that aspect to a string, and then puts those strings together to make a row in the ListView. For those who think in pictures, you can visualize the process like this:
SetObjects()
For those of you who have struggled with a ListView before, you must unlearn. An ObjectListView is not a drop in replacement for a ListView. If you have an existing project, you cannot simply create an ObjectListView instead of creating a ListView. An ObjectListView needs a different mind-set. If you can perform the mind-mangling step of changing your thinking, ObjectListView will be your best friend.
An ObjectListView is much more active than a plain ListView. A normal ListView is essentially passive: it sits there, and you poke and prod it and eventually it looks like you want. With an ObjectListView you tell it what you want done and the ObjectListView does it for you. More formally: An ObjectListView is used declaratively. You configure the ObjectListView, give it your collection of model objects, and the ObjectListView builds the ListView for you.
The crucial part of using an ObjectListView is configuring it. Most of this configuration can be done within the IDE by setting properties on the ObjectListView itself or on the columns that are used within the list. Some configuration cannot be done through properties: these more complex configurations are done by responding to events or by installing delegates (more on this later). Once the columns and control are configured, putting it into action is simple, as you have already seen: a single call to SetObjects().
Beware of ListViewItems. You never need to add ListViewItems to an ObjectListView. If you find yourself adding things to the Items collection, creating ListViewItems, or adding sub-items to anything, then you need to stop — you are being seduced to the dark side. An ObjectListView does all that work for you. It owns the ListViewItems, and destroys, changes and builds them as required from the information you have given. Resist the temptation to add/edit/sort or otherwise mess with ListViewItems — it will not work.
Items
There is also no need to hide information in a ListViewItem. Old style ListView programming is often required attaching a key of some sort to each ListViewItem, so that when the user did something with a row, the programmer would know which model object that row was related to. This attaching was often done by creating one or more zero-width columns, or by setting the Tag property on the ListViewItem. With an ObjectListView, you do not need to do this anymore. The ObjectListView already knows which model object is behind each row. In many cases, the programmer simply uses the SelectedObjects property to find out which model objects the user wants to do something to.
ListViewItem
Tag
SelectedObjects
A single call to SetObjects() is all well and good, but real-world applications need more than just sorting and grouping. They need at least a little image in the first column.
The obvious first enhancement to this simple example is to display images in the ListView. To do that, we need to configure the ObjectListView so it knows what image to show against each row. This normally cannot be done within the IDE. Very often the image to be shown depends on the model object being displayed. To decide on an image, we need a more complex type of configuration: installing a delegate.
A delegate is basically a piece of code that you give to an ObjectListView saying, "When you need to do this, call this piece of code" where this can be any of several tasks. In this case, we install an ImageGetter delegate, which tells the ObjectListView, "When you need to figure out the image for this model object, call this piece of code." [If the word "delegate" worries you, think of them as function pointers where the parameter and return types can be verified. If that makes no sense to you, just keep reading. It will (possibly) become clear with some examples.]
ImageGetter
First, you need a method that matches the ImageGetterDelegate signature: it must accept a single object parameter and returns an object. The value returned from the ImageGetter delegate is used as an index into the ObjectListView's SmallImageList. As such, the ImageGetter can return either a string or an int. (If the ObjectListView is owner-drawn, the ImageGetter can also return an Image).
ImageGetterDelegate
object
ObjectListView's
SmallImageList
string
int
Image
A somewhat frivolous example follows:
object PersonColumnImageGetter (object rowObject) { // People whose names start with a vowel get a star, // otherwise the first half of the alphabet gets hearts // and the second half gets music Person p = (Person)rowObject; if ("AEIOU".Contains(p.Name.Substring(0, 1))) return 0; // star else if (p.Name.CompareTo("N") < 0) return 1; // heart else return 2; // music };
To install it, you do this:
this.personColumn.ImageGetter = new ImageGetterDelegate(this.PersonColumnImageGetter);
In VB:
this.personColumn.ImageGetter = New ImageGetterDelegate(AddressOf PersonColumnImageGetter)
delegate
this.personColumn.ImageGetter = delegate (object rowObject) { Person p = (Person)rowObject; if ("AEIOU".Contains(p.Name.Substring(0, 1))) return 0; // star else if (p.Name.CompareTo("N") < 0) return 1; // heart else return 2; // music };
Anonymous delegates save you from having to add lots of little methods to your class. However, if the anonymous delegates start to become too big or if you find yourself copying them verbatim from one place to another, it's a good sign that you need to put some new methods on your model class.
[v2.3] If your model class has a property which can return the name or the index of the image that should be shown, you don't need to install a delegate. You can set the ImageAspectName property to the name of that property. But this is crossing the line between model and view, so I'm not encouraging this practice, just pointing out that it's possible.
ImageAspectName
ObjectListView uses a combination of events and delegates to allow further, more complex customizations. All of the following can be customized through delegates:
OLVColumn.AspectGetter
AspectGetter
OLVColumn.AspectToStringConverter
ToString()
OLVColumn.AspectPutter
OLVColumn.GroupKeyGetter
OLVColumn.GroupKeyToTitleConverter
OLVColumn.CheckStateGetter
CheckStatePutter
ObjectListView provides many events, which allow the programmer to customize its behaviour. Have a look at events available in the IDE. Some commonly used events are:
FormatRow
FormatCell
CellToolTipShowing
HeaderToolTipShowing
SelectionChanged
SelectedIndexChanged
The complex example tab in the demo project has examples of using all of these delegates. For example, turn on "Show Groups" and click on the "Cooking Skill" or "Hourly Rate" column to see what's possible.
The control is written to be data-agnostic. It doesn't care what type of data it is working on. The only requirement is that the object passed to the SetObjects method must support the IEnumerable interface, which isn't too heavy a requirement. This means that it works equally well with an ArrayList, a DataTable or a list of compiler errors returned from CompilerResults.Errors. For example, to display information from DataTable, you could install AspectGetter delegates like this:
SetObjects
IEnumerable
ArrayList
DataTable
CompilerResults.Errors
columnName.AspectGetter = delegate(object row) { return ((DataRow)row)["Name"]; }; columnCompany.AspectGetter = delegate(object row) { return ((DataRow)row)["Company"]; }; columnAge.AspectGetter = delegate(object row) { return ((DataRow)row)["Age"]; };
Then install the table like this:
this.objectListView1.SetObjects(ds1.Tables["Persons"].Rows);
Note that this code installed the Rows, not the DataTable itself.
Rows
Actually, you would not have to define AspectGetters to do this. It would be enough to set columnName.AspectName to "Name" and the ObjectListView would be able to extract the indexed property from the DataRow. But the example still works as it stands.
AspectGetters
columnName.AspectName
DataRow
In response to intense public demand — OK, a couple of people asked for it — there is a DataListView class. This is a data-bindable version of ObjectListView which accepts various kinds of data sources and populates the list with data from that source.
For those programmers for whom even one line of code is too much, DataListView can be configured completely within the IDE. Give it a DataSource, set up the columns and it will simply work. DataListView also listens for ListChanged events on its data source and uses those to keep its list in sync with the contents of the data source. Add a new row to the data table and it will automatically appear in the ListView! Edit a value in the DataListView and the change automatically appears in the DataTable!
DataSource
ListChanged
The DataListView can accept several types of objects as data sources:
DataView
DataSet
DataViewManager
BindingSource
To use DataListView, you give each column the name of the data column you want it to display in the AspectName property. Then set the DataSource member to your data source. That's it! Even more slothfulness! So, we could accomplish the same thing as the "data unaware" example above by configuring AspectNames for the columns and then setting the data table directly, like this:
AspectName
AspectNames
this.dataListView1.DataSource = ds1.Tables["Persons"];
Alternatively, for the monumentally slothful, the whole thing could be done through IDE configuration. Set the AspectName property on the columns; set the DataSource property to the appropriate dataset and then set the DataMember property to tell which table from the dataset you want to show. Hey, presto, a fully functional data set viewer. All without writing a single line of code.
DataMember
[2.5] There is a now FastDataListView, which combines the ease of use of a DataListView with the speed of a FastObjectListView. This basically manages a datasets through a virtual list. On my mid-range laptop, it can handle data sets of 100,000+ rows effortlessly.
If you've ever wanted to thoroughly overwhelm your users with 10 million rows of data, then go ahead and knock them out with VirtualObjectListView.
Let's get this out of the way first: ListViews are NOT good interfaces for large number of items. There aren't any. If you are trying to show a ListView that has 10 million rows, you need to rethink your interface. However, if you really have to use a ListView with that many rows, then VirtualObjectListView is your answer.
ListViews
Normally ObjectListView keeps a list of model objects that it can read, sort or group at will. An VirtualObjectListView does not keep such a list. Instead, it only fetches model objects when they need to be displayed. For large lists, this is a massive reduction in resources. If the user never looks at the 4-millionth row, the VirtualObjectListView will never ask for it, so the program will never have to create it.
To use a VirtualObjectListView, you must implement an IVirtualListDataSource and give that data source to the virtual list (via the VirtualListDataSource property). Using that interface, the virtual list can then function just like a full-fledged ObjectListView. The only things a virtual list still can't do are: it can't show groups and it can't use tile view. But otherwise they should operate in the same way as a normal ObjectListView, including sorting, check boxes, and searching through typing.
VirtualListDataSource
If you don't want to implement all the methods of IVirtualListDataSource, you can subclass AbstractVirtualListDataSource which is a "do-nothing" implementation of that interface. At very least, you must implement GetObjectCount() and GetNthObject(), otherwise nothing will appear in the list.
AbstractVirtualListDataSource
GetObjectCount()
GetNthObject()
Although it isn't documented, .NET virtual ListViews cannot have checkboxes. VirtualObjectListView codes around this limitation, but you must use the functions provided by ObjectListView: CheckedObjects, CheckObject(), UncheckObject() and their friends. If you use the normal check box properties (CheckedItems or CheckedIndicies), they will throw an exception, since the ListView is in virtual mode, and .NET "knows" it can't handle checkboxes in virtual mode.
CheckedObjects
CheckObject()
UncheckObject()
CheckedItems
CheckedIndicies
So far, ObjectListView has been catering to the slothful, those of us who want to do the least work and get the most results. If impatience is your key character flaw, then the FastObjectListView is for you. In exchange for losing a few features, you gain a great amount of speed.
How fast is it? On my low-range laptop, a normal ObjectListView builds a list of 10,000 objects in about 10 seconds. A FastObjectListView builds the same list in less than 0.1 seconds.
What do you lose? With a FastObjectListView, you cannot use Tile view and if you are on XP, you cannot show groups. Apart from that, all features of ObjectListView are available in the FastObjectListView. From v2.3 onwards, when running on Vista or later, FastObjectListViews can show groups. Simply set ShowGroups to true, and the control will handle groups in the same fashion as a normal ObjectListView.
FastObjectListViews
ShowGroups
From time to time, there are situations when you want to show a tree structure (like a TreeView), but you also want to show more than information about the items than just their name (like a ListView). Enter the TreeListView. It shows a tree structure with its nice ability to expand and collapse, but also shows information in columns:
TreeView
Like all the other ObjectListViews, TreeListView relies on delegates. The two essential delegates for using a TreeListView are:
ObjectListViews
to know if a given model can be expanded: CanExpandGetter delegate.
CanExpandGetter
to get the children of a model when it is expanded: ChildrenGetter delegate.
ChildrenGetter
In the demo, there is an Explorer like example, which navigates the disks on the local computer. The tree list view in that demo is configured like this:
this.treeListView.CanExpandGetter = delegate(object x) { return (x is DirectoryInfo); }; this.treeListView.ChildrenGetter = delegate(object x) { DirectoryInfo dir = (DirectoryInfo)x; return new ArrayList(dir.GetFileSystemInfos()); };
In this example, the CanExpandGetter delegate ensures that only directories can be expanded.
The ChildrenGetter delegate returns the contents of a directory when that directory is expanded. ChildrenGetter delegates are only ever called if CanExpandGetter returns true. So in this case, the ChildrenGetter delegate knows that the parameter x must be a DirectoryInfo instance.
true
x
DirectoryInfo
To make it work, you must add some "roots" (top level objects). You can do this either by setting the Roots property to a collection of model objects, or you can just call SetObjects() like normal. On a TreeListView, SetObjects(), AddObject() and RemoveObject() all apply to the collection of roots.
Roots
AddObject()
RemoveObject()
To refresh the list of children under a model, you call RefreshObject() on the parent.
RefreshObject()
TreeListView works best when there is a significant overlap of information between branches and the leaf nodes. They have to share the same columns. They also work when the branches have no information apart from their name and the columns are really only used for the leaf nodes. They do not work so well when you want to show several bits of information about branches and other bits of information about leaf nodes, with little overlap between the two. They look silly because of all the empty cells.
One annoyance with ObjectListView is all the casting that is needed. Because the ObjectListView makes no assumptions about what sort of model objects you will be using, it handles all models as objects and it's up to you to cast them to the right type when you need to. This leads to many delegates starting with a cast like this:
objects
this.objectListView1.SomeDelegate = delegate(object x) { MyModelObject model = (MyModelObject)x; ... }
which becomes tiresome after a while. It would be nice if you could tell the ObjectListView that it would always be displaying, say, Person objects. Something like:
Person
this.objectListView1 = new ObjectListView<Person>(); this.objectListView1.SomeDelegate = delegate(Person model) { ... }
Unfortunately, the designer in Visual Studio cannot handle parameterized controls like that. [I remember reading that in an Microsoft blog somewhere, but I can't find it again. There are a couple of knowledgeable people who says that it can't - here for example. If someone knows if this is a documented decision, could you please let me know.] There are a couple of tricks to get around some of the most obvious problems, but they all hit a wall with the code generation.
So, in the meantime, we now have a TypedObjectListView class. This is not another ObjectListView subclass, but rather it's a typed wrapper around an existing ObjectListView. To use one, you create an ObjectListView within the IDE as normal. When it is time to implement your delegates, you create a TypedObjectListView wrapper around your list view, and declare your delegates against that wrapper. It's easier to use than it is to explain, so look at this example:
TypedObjectListView
TypedObjectListView<Person> tlist = new TypedObjectListView<Person>(this.listViewSimple); tlist.BooleanCheckStateGetter = delegate(Person x) { return x.IsActive; }; tlist.BooleanCheckStatePutter = delegate(Person x, bool newValue) { x.IsActive = newValue; return newValue; };
Look ma! No casts! The delegates are declared against the typed wrapper, which does know what model objects are being used.
You can also use the TypedObjectListView for typed access to the delegates on your columns:
tlist.GetColumn(0).AspectGetter = delegate(Person x) { return x.Name; }; tlist.GetColumn(1).AspectGetter = delegate(Person x) { return x.Occupation; };
If you don't like referring to columns by their index, you can create TypedColumn objects around a given ColumnHeader object:
TypedColumn
ColumnHeader
TypedColumn<Person> tcol = new TypedColumn<Person>(this.columnHeader16); tcol.AspectGetter = delegate(Person x) { return x.GetRate(); }; tcol.AspectPutter = delegate(Person x, object newValue) { x.SetRate((double)newValue); };
The final feature of a TypedObjectListView is that it can automatically generate an AspectGetter for a column from its AspectName. So, rather than hand-coding AspectGetters like we have done above, you simply configure the AspectName in the IDE, and then call tlist.GenerateAspectGetters(). This can (should?) handle aspects of arbitrary complexity, like "Parent.HomeAddress.Phone.AreaCode".
tlist.GenerateAspectGetters()
Parent.HomeAddress.Phone.AreaCode
Sometimes it makes no sense to allow the user to resize a column. A column that simply shows a 16x16 status icon has no reason to be resizable. Extending this idea one step further, you can imagine cases where a column should not be less than a given size or wider than a maximum size. So, it would be good if we could give columns a minimum and a maximum width. Setting the same value for both would give a fixed-sized column.
However, controlling the resizing of columns turns out to be a non-trivial problem. It is easy to find examples of fixing the width of all columns in a ListView: Chris Morgan has a nice implementation available here. Unfortunately, that technique cannot be used to restrict individual column widths. In fact, I could not find any example anywhere of how to restrict a column width to be within a given range.
Regardless, columns can be given a MinimumWidth and a MaximumWidth. Even within the IDE, these settings will prevent the user from setting the width of the column to outside of the given values. See below for a more detailed discussion of the complexities and potential limitations of my implementation.
MinimumWidth
MaximumWidth
There are situations where it would be nice if a column (normally the rightmost one) would expand as the listview expands, so that as much of the column was visible as possible without having to scroll horizontally (you should never, ever make your users scroll anything horizontally!). A free space filling column does exactly that. The "Comments" column in the Simple tab of the demo shows this in action.
listview
When an ObjectListView is resized, the space occupied by all the fixed width columns is totaled. The difference between that total and the width of the control is then shared between the free space filling columns. If you only have one such column, it is given all the space; if you have two, each is given half the space.
Be a little cautious with these space filling columns. Their behaviour is not standard and can sometimes be quite surprising, especially if the columns aren't the right most columns. One surprise is that these columns cannot be resized by dragging their divider — their size depends on the free space available in the ListView.
Now that you've gone to all that work to make a very pretty ListView, wouldn't it be nice if you could just print it? Yes, I know there is always the PrntScrn key, but I have noticed that some upper management do not think very highly of that as a reporting solution.
The ListViewPrinter is the answer to your printing problem. Configure an instance of it in the IDE (the ListView property controls which list is printed) and then call:
ListViewPrinter
this.listViewPrinter1.PrintPreview();
Thus, for nothing you can have a very pretty report that looks like this one.
Admittedly, the formatting in this example is too much, but you can modify all the formatting to suit your tastes. See the demo for some more sedate examples and read the code to see how to make it work. It really is very cool.
This is a logically separate piece of code, so it lives in its own project. If you want to use it, you will need to add to your project either the ListViewPrinter project itself or the ListViewPrinter.dll file. The procedure is the same as for the ObjectListView project given in the First Steps section above.
ListViews are normally used for displaying information. The standard ListView allows the value at column 0 (the primary cell) to be edited, but nothing beyond that. ObjectListView allows all cells to be edited. Depending on how the data for a cell is sourced, the edited values can be automagically written back into the model object.
The "editability" of an ObjectListView is controlled by the CellEditActivation property. This property can be set to one of the following values:
CellEditActivation
CellEditActivateMode.None
CellEditActivateMode.SingleClick
CellEditActivateMode.DoubleClick
CellEditActivateMode.F2Only
Individual columns can be marked as editable via the IsEditable property (default value is true), though this only has meaning once the ObjectListView itself is editable. If you know that the user should not be allowed to change cells in a particular column, set IsEditable to false. Be aware, though, that this may create some UI surprises (resulting in complaints like "How come I can't edit this value by clicking on it like I can on all the other cells?"). You have been warned.
IsEditable
false
Once a cell editor is active, the normal editing conventions apply:
The default processing creates a cell editor based on the type of the data in the cell. It can handle bool, int, string, DateTime, float and double data types. When the user has finished editing the value in the cell, the new value will be written back into the model object (if possible).
bool
DateTime
float
double
To do something other than the default processing, you can listen for two events: CellEditStarting and CellEditFinishing.
CellEditStarting
CellEditFinishing
The CellEditStarting event is triggered after the user has requested to edit a cell but before the cell editor is placed on the screen. This event passes a CellEditEventArgs object to the event handlers. In the handler for this event, if you set e.Cancel to true, the cell editing operation will not begin. If you don't cancel the edit operation, you will almost certainly want to play with the Control property of CellEditEventArgs. You can use this to customise the default editor, or to replace it entirely.
CellEditEventArgs
e.Cancel
Control
For example, if your ObjectListView is showing a Color in a cell, there is no default editor to handle a Color. You could make your own ColorCellEditor, set it up correctly, and then set the Control property to be your color cell editor. The ObjectListView would then use that control rather than the default one. If your cell editor has a read/write property called Value, ObjectListView will use that to get and put the cell value into the control. If it doesn't, the Text property will be used instead.
Color
ColorCellEditor
Value
Text
When the user wants to finish the edit operation, a CellEditFinishing event is triggered. If the user has cancelled the edit (e.g. by pressing Escape), the Cancel property will already be set to true. In that case, you should simply cleanup without updating any model objects. If the user hasn't cancelled the edit, you can by setting Cancel to true — this will force the ObjectListView to ignore any value that the user has entered into the cell editor.
Cancel
No prizes for guessing that you can refer to the Control property to extract the value that the user has entered and then use that value to do whatever she or he wants. During this event, you should also undo any event listening that you have setup during the CellEditStarting event.
You can prevent the cell edit operation from finishing (e.g. if the value the user has entered isn't acceptable) by listening for the CellEditValidating event. If the handler for this event sets Cancel to true, the edit operation will NOT finish and the editor will remain on the screen. Please make sure you have made it clear to the user why the edit operation hasn't finished.
CellEditValidating
You can look in the demo at listViewComplex_CellEditStarting() and listViewComplex_CellEditFinishing() to see an example of handling these events.
listViewComplex_CellEditStarting()
listViewComplex_CellEditFinishing()
Once the user has entered a new value into a cell and pressed Enter, the ObjectListView tries to store the modified value back into the model object.
There are three ways this can happen:
RefreshItem()
OLVColumn
AspectPutter
Owner.Address.Postcode
If none of these three things happen, the user's edit will be discarded. The user will enter her or his new value into the cell editor, press Enter, and the old value will be still be displayed. If it seems as if the user cannot update a cell, check to make sure that one of the three things above is occurring.
All aspects of cell editing are described in further details on this page.
Remember that can of worms I didn't want to open? Owner drawing the ListView? Well, one afternoon when I had too little to do (ha!), I decided that it really couldn't be too bad and I got out my can opener. Several evenings later, I could only confirm my original estimate: owner drawing is a can of worms. It should be easy. It should just work. But it doesn't.
Regardless, ObjectListViews can now be owner-drawn and it is owner drawing on steroids! Like most of ObjectListView, owner drawing is accomplished by installing a delegate. Inside the renderer delegate, you can draw whatever you like:
columnOD.RendererDelegate = delegate(DrawListViewSubItemEventArgs e, Graphics g, Rectangle r, Object rowObject) { g.FillRectangle(new SolidBrush(Color.Red), r); g.DrawString(((Person)rowObject).Name, objectListView1.Font, new SolidBrush(Color.Black), r.X, r.Y); }
Installing a delegate works fine, but there are numerous utility methods that are useful within such a delegate. Is the row currently selected? What colour should the background be? The BaseRenderer class encapsulates these utilities. To make your own Renderer class, you must subclass BaseRenderer, override the Render(Graphics g, Rectangle r) method and again draw whatever you like, only this time you have a lot of nice utility methods available to you. There are a couple of subclasses of BaseRenderer already available for use.
BaseRenderer
Renderer
Render(Graphics g, Rectangle r)
BarRenderer
MultiImageRenderer
MappedImageRenderer
enum
ImageRenderer
ICollection
strings
ints
Images
FlagsRenderer
To use any of these renderers or your own custom subclass, you assign an instance of them to a column's Renderer property, like this:
colCookingSkill.Renderer = new MultiImageRenderer(Resource1.star16, 5, 0, 40);
This means that the cooking skill column will draw up to 5 of the star16 images, depending on the data value. The renderer expects data values in the range 0 to 40. Values of 0 or less will not draw any stars. Values of 40 or more will draw 5 stars. Values in between will draw a proportional number of stars.
star16
As of v2.0, Renderers are now Components, which means they can created and manipulated within the IDE. So, to use a MultiImageRenderer like the above, you would create one within the IDE, configure its properties to be as you can, and then assign it to the column's Renderer property.
Renderers
Components
Owner drawing only happens when you turn on the OwnerDrawn mode. So, you can only see your custom renderer when the ObjectListView is in owner-drawn mode.
OwnerDrawn
Rows in list views are always of fixed height and calculated from the ListView font and/or the height of the image lists. Row height can be set using the RowHeight property. You cannot have rows of differing heights — it simply cannot be done with a ListView.
RowHeight
It is obvious, but easily overlooked, that owner drawing is slower than non-owner drawing. Owner drawing requires a lot more work than native drawing. Again, for small lists, the difference is not significant. However, it can be noticeable when a large number of redraws is necessary. For example, go to the "Virtual List" tab on the demo and drag the scroll thumb down to the bottom. Now turn on OwnerDraw and do it again. Quite a difference!
OwnerDraw
As of v2.2, ObjectListView now has reasonably sophisticated support for drag and drop operations.
If you want the user to be able to drag rows out of an ObjectListView, you set the DragSource property. This property accepts an object that implements the IDragSource interface. It will often be enough to use an instance of SimpleDragSource:
DragSource
IDragSource
SimpleDragSource
this.objectListView1.DragSource = new SimpleDragSource();
This drag source remembers the currently selected rows, and equips the drag data object with text and HTML versions of those rows. With that simple drag source, you can select 10 rows from an ObjectListView and drag them onto Microsoft Word to create a formatted table of those rows. You can also drag rows onto other ObjectListViews, which will normally be what you want.
From within the IDE, you can set IsSimpleDragSource to true to make your ObjectListView into a drag source using a SimpleDragSource.
IsSimpleDragSource
Accepting drops from other sources is a little more complicated, but is handled in similar manner. If you want the user to be able to drop stuff onto an ObjectListView, you set the DropSink property. This property accepts an object that implements the IDropSink interface. In many cases, you will use an instance of SimpleDropSink.
DropSink
IDropSink
SimpleDropSink
this.objectListView1.DropSink = new SimpleDropSink();
From within the IDE, you can set IsSimpleDropSink to true to do make your ObjectListView into a drop sink. A DragSource needs no further information, but a DropSink needs to know at least two other things:
IsSimpleDropSink
If you use a SimpleDropSink, the ObjectListView will trigger two events to handle these situations: a CanDrop event and a Dropped event. To actually be useful, you need to handle these events. You can set up handlers for these events within the IDE, like normal.
CanDrop
Dropped
You can alternatively listen for the ModelCanDrop and ModelDropped events. This second pair of events are triggered when the source of the drag is another ObjectListView. These events work the same as the CanDrop and Dropped events except that the argument block includes more information:
ModelCanDrop
ModelDropped
A SimpleDropSink is actually quite sophisticated. It can be configured in several ways. Have a look at the code to see more options.
Allow drops on the background:
myDropSink.CanDropBetween = true;
Allow drops between items:
You can even drop on subitems:
myDropSink.CanDropOnSubItems = true;
And change highlight colour just to be different:
myDropSink.FeedbackColor = Color.IndianRed;
You can learn more about drag and drop, including how to write your own drop sink from scratch on this page.
The most frequently requested feature is collapsible groups. I want it; you want it; your great aunt Mildrid who lives in Wollongong wants it. Unfortunately, with the current ListView it is just not possible: groups cannot be collapsed — on XP. But on Vista, this most commonly requested feature is a reality. It is enabled by default, so under Vista, groups are automatically collapsible. If you don't want your groups to be collapsible, set HasCollapsibleGroups to false. Thanks to Crustyapplesniffer who implemented this feature.
HasCollapsibleGroups
In v2.3, groups received a major overhaul. No longer content with just being collapsible, groups can now have a title image, a subtitle, a task (that clickable link on the right), and footers. When done well, this can make your listview look very nice indeed:
This extended formatting can be setup during the AboutToCreateGroup event. Alternatively, you can use the extended version of the MakeGroupies() method, which allows all these new properties to be configured. The above screenshot was configured with one MakeGroupies() call:
AboutToCreateGroup
MakeGroupies()
this.columnCookingSkill.MakeGroupies( new object[]{10, 20, 30, 40}, new string[] {"Pay to eat out", "Suggest take-away", "Passable", "Seek dinner invitation", "Hire as chef"}, new string[] { "emptytoast", "hamburger", "toast", "dinnerplate", "chef" }, new string[] { "Pay good money -- or flee the house -- rather than eat their homecooked food", "Offer to buy takeaway rather than risk what may appear on your plate", "Neither spectacular nor dangerous", "Try to visit at dinner time to wrangle an invitation to dinner", "Do whatever is necessary to procure their services" }, new string[] { "Call 911", "Phone PizzaHut", "", "Open calendar", "Check bank balance" } );
These group formatting facilities are only available on Vista and later. On XP, groups can only have a header.
When an ObjectListView is empty, it can display a "this list is empty" type message. The EmptyListMsg property holds the string that appears when an ObjectListView is empty. This string is rendered using the EmptyListMsgFont. Both of these properties can be configured within in the IDE.
EmptyListMsg
EmptyListMsgFont
But if you want to write a little bit of code, you can have much more interesting messages. The empty list message is actually implemented as an overlay. You can access that overlay though the EmptyListMsgOverlay property. By default, this is a TextOverlay that you can customise to your hearts content:
EmptyListMsgOverlay
TextOverlay
TextOverlay textOverlay = this.objectListView1.EmptyListMsgOverlay as TextOverlay; textOverlay.TextColor = Color.Firebrick; textOverlay.BackColor = Color.AntiqueWhite; textOverlay.BorderColor = Color.DarkRed; textOverlay.BorderWidth = 4.0f; textOverlay.Font = new Font("Chiller", 36); textOverlay.Rotation = -5;
Doing this gives a message like this:
ObjectListViews can now treat cells as hyperlinks. To do this, set UseHyperlinks to true of the ObjectListView, and then set Hyperlink property of OLVColumn to true to make all the cells in that column behaves as hyperlinks.
UseHyperlinks
Hyperlink
If you don't want all cells to be hyperlinks, you can listen for the IsHyperlink event (in the above shot, occupation that start with "s" are not hyperlinks). In this event, you can specify what URL will be attached to that cell. By default, the URL is the text of the cell. If you set the URL to null that cell will not be treated as a hyperlink. If you are already listening for the FormatCell you could set up the URL in that event too.
IsHyperlink
The formatting of the hyperlinks is controlled by the HyperlinkStyle property of ObjectListView. You can create and configure a HyperLinkStyle within the IDE, and then assign it to your ObjectListView. The same style can be assigned to multiple ObjectListViews. In 95% of cases, the default styling will suffice.
HyperlinkStyle
HyperLinkStyle
When a hyperlink is clicked, a HyperlinkClicked event is triggered. If you handle this yourself, set Handled to true to prevent the default processing from occurring. If you don't handle it, the default processing is to try and open the associated URL.
HyperlinkClicked
Handled
Be careful about making column 0 to be a hyperlink. If it is, every time a user clicks a row trying to select it, it will open a browser window, which would become annoying very quickly.
In v2.4, the headers of an ObjectListView became fully style-able. If ObjectListView.HeaderUsesTheme is true (the default), the header will be drawn according to the OS's current theme and will ignore any header style. If this is false, the headers will be formatted according to their given header format style.
ObjectListView.HeaderUsesTheme
You can set the style for all columns at once (through ObjectListView.HeaderFormatStyle) or for just one column (through OLVColumn.HeaderFormatStyle). A style given to a specific column takes precedence over one given to the control as a whole. Like other styles, HeaderFormatStyles can be created, configured and assigned within the IDE.
ObjectListView.HeaderFormatStyle
OLVColumn.HeaderFormatStyle
HeaderFormatStyles
Header styles allow a different appearance for each state of the header:
Normal
Hot
Pressed
For each state, the header format allows the font, font color, background color and frame to be specified. If you combine these attributes badly, you can produce some truly dreadful designs, but when well used, the effect can be pleasant.
There is also HeaderWordWrap on ObjectListView which allows the text within a header to be word wrapped. So, if you are feeling sadistic, you can inflict something like this on your users:
HeaderWordWrap
If you have a narrow column and want to save some horizontal space, you can set OLVColumn.IsHeaderVertical to true, and the text of the header will be drawn vertically:
OLVColumn.IsHeaderVertical
.NET has ImageIndex and IndexKey properties on the ColumnHeader class. However, no one knows quite why since any value you set on these in the IDE will not be persisted by the code generator.
ImageIndex
IndexKey
ObjectListView, however, has the HeaderImageKey property, which allows you to choose the image that will be shown in the header of a column.
HeaderImageKey
One fundamental of good design is separation of presentation from model. Model classes should not know how they are being presented to the user.
But there are development situations when speed of development is everything (Merchant banks and stock brokers often seem to be in this camp). In such cases, placing some sort of user interface into the model classes themselves is an acceptable trade off.
It is with a nod to such development that ObjectListView now has Generator class and OLVColumn attributes. The idea with these classes is that, in your model classes, you decide which properties you want to appear in the ObjectListView, and then you give those properties an OLVColumn attribute. In these attribute, you specify some of the characteristics that you would normal give through the IDE (e.g. column title, alignment, image getter, format string). Then, when you are ready to show your list of models, you generate the columns from the model and then show the models:
Generator
List<ForexPurchase> purchases = this.GetForexPurchasesToShow(); Generator.GenerateColumns(this.olv, purchases); this.olv.Objects = purchases;
In this example, this.olv is a completely unconfigured ObjectListView. Nothing was done to it in the IDE, except for placing it on the form: no columns were created or configured. The Generator uses the information that was given in the OLVColumn attributes to build a fully functional ObjectListView.
this.olv
When the user later wants to see the foreign exchange sales that were made today, she clicks the "Sales" button, and some code like this might be executed:
List<ForexSale> sales = this.GetForexSalesToShow(); Generator.GenerateColumns(this.olv, sales); this.olv.Objects = sales;
This reuses the same ObjectListView control, but now it is a fully functional ObjectListView showing information about Forex sales.
[Thanks to John Kohler for this idea and the original implementation]
In v2.4, ObjectListViews became filterable. For backwards compatibility, this ability is off by default. Set UseFiltering to true to enable this feature.
UseFiltering
If you set the ModelFilter or ListFilter properties, only model objects that match those filters will be shown in the list. List filters apply some criteria on the list as a whole. A tail filter (showing only,say, the last 500 lines) is a good example of a whole list filter (this is implemented by the TailFilter class). A model filter considers each model object in turn and decides whether or not that model should be included in the list shown to the user. Model filters are the most commonly used.
ModelFilter
ListFilter
TailFilter
The ModelFilter provides a very useful filtering implementation. It takes a delegate as a construction parameter, and that delegate decides if the given model object should be included. To filter a list of phone calls to only show emergency calls, you could install filter like this:
this.olv1.ModelFilter = new ModelFilter(delegate(object x) { return ((PhoneCall)x).IsEmergency; });
You can, of course, make your own filters, by implementing the IModelFilter or the IListFilter interface. You could, for example, show only emergency calls like this:
IModelFilter
IListFilter
public class OnlyEmergenciesFilter : IModelFilter { public bool Filter(object modelObject) { return ((PhoneCall)x).IsEmergency; } } ... this.olv1.ModelFilter = new OnlyEmergenciesFilter();
One very common filtering task is to only show rows that contain a certain string. iTunes does this with its "Search" box. ObjectListView makes it very easy to implement this text filtering via a TextMatchFilter. You use it thus::
TextMatchFilter
this.olv1.ModelFilter = new TextMatchFilter(this.olv1, "search");
After executing this line, the ObjectListView will only show rows where the text "search" occurs in at least one cell of that row.
The filter can be configured to only consider some of the columns in the ObjectListView by setting the Columns property. This is useful for avoiding searching on columns that you know will return nonsensical results (like checkboxes or image only columns).
Columns
It can also be set up to do regular expression searching or simple prefix matching:
this.olv1.ModelFilter = new TextMatchFilter(this.olv1, "^[0-9]+", TextMatchFilter.MatchKind.Regex);
As a bonus, if your filtered ObjectListView is owner drawn, you can pair this text searching with a special renderer, HighlightTextRenderer. This renderer draws a highlight box around any substring that matches its given text. So:
HighlightTextRenderer
TextMatchFilter filter = new TextMatchFilter(this.olv1, "er"); this.olv1.ModelFilter = filter; this.olv1.DefaultRenderer = new HighlightTextRenderer(filter);
would give something that looks like this:
You can change the highlighting by playing with the CornerRoundness, FramePen and FillBrush properties on the HighlightTextRenderer.
CornerRoundness
FramePen
FillBrush
Remember: the list has to be owner drawn for the renderer to have any effect.
[v2.5] ObjectListView can present the user with an Excel-like filtering interface. If they right click on a column's header, a "Filtering" menu item will be presented. This will let the user select one or more distinct values from that column. When they click "Apply", only those rows that have one of the chosen values for that column will be displayed.
If you don't want your users to have this filtering ability, set ObjectListView.ShowFilterMenuOnRightClick to false. To hide the ‘Filter’ menu item for a particular column, set UsesFiltering to false on that column.
ObjectListView.ShowFilterMenuOnRightClick
UsesFiltering
At runtime, the user is able to select which columns they wish to see in an ObjectListView. The user interface mechanism for this is that when the user right clicks on any header, they will presented with a menu that lets them choose which columns they wish to see.
The exact behaviour of the column selection mechanism is governed by the SelectColumnsOnRightClickBehaviour property.
SelectColumnsOnRightClickBehaviour
To prevent the user from changes the visible columns, set this property to ColumnSelectBehaviour.None.
ColumnSelectBehaviour.None
To present the column selection menu as a submenu off the header right click menu, set this property to ColumnSelectBehaviour.Submenu.
ColumnSelectBehaviour.Submenu
To present the column selection menu as the bottom items in the header right click menu, set this property to ColumnSelectBehaviour.Inline. This is the default. If SelectColumnsMenuStaysOpen is true (which is the default), the menu will remain open after the user clicks on column, letting them hide or show multiple columns without having to right click again.
ColumnSelectBehaviour.Inline
SelectColumnsMenuStaysOpen
To present the user with a dialog that lets them choose the columns (as well as rearrange the order of the columns), set this property to ColumnSelectBehaviour.ModelDialog.
ColumnSelectBehaviour.ModelDialog
If there are some columns that you do not want the user to be able to hide, set OLVColumn.Hideable to false. This will prevent the user from hiding that column.
OLVColumn.Hideable
Note: Column 0 can never be hidden. This is a limit of the underlying Windows control. If you wish to make your first column hideable, move it to anywhere else in the column list, and then set its DisplayIndex to 0, so that it appears first.
DisplayIndex
Haven't you always wanted to put those little snazzy bits of moving eye candy into your applications? You know, a flashing border around some cell, or a spinning star right in the middle of the list?
In v2.4, ObjectListView now integrates with the Sparkle animation library. This library lets you put animations over the top of existing Controls. Due to the way .NET ListViews work, that library cannot work with plain ListViews. However, through the extendibility of ObjectListViews decorations, it now works seemlessly with ObjectListViews.
To learn about the Sparkle library, you should read this article -- and of course look at the code. But briefly, the design goals of the library were:
To use the library itself, you'll need to grasp its four major concepts:
Sprite
ISprite
Effects
IEffect
The workflow for creating a Sparkle animation is:
Animation
Sprites
Effect
Locators
OK, OK. Just show me the code. Let's put a spinning, fading star in the middle of an ObjectListView.
To put animations onto an ObjectListView, you use AnimatedDecoration class. Depending on the constructor used, this will create an animation for the whole list, for a row, or for just one cell:
AnimatedDecoration
AnimatedDecoration listAnimation = new AnimatedDecoration(this.olvSimple); AnimatedDecoration rowAnimation = new AnimatedDecoration(this.olvSimple, myModel); AnimatedDecoration cellAnimation = new AnimatedDecoration(this.olvSimple, myModel, olvColumn);
We want an animation that can draw on the whole list so we use this form:
AnimatedDecoration listAnimation = new AnimatedDecoration(this.olvSimple); Animation animation = listAnimation.Animation;
Now we have an Animation, we can make our Sprites. We only need one sprite and we want it to show an image, so we use an ImageSprite. There is also a TextSprite for drawing bordered/backgrounded text, and ShapeSprite for drawing regular shapes.
ImageSprite
TextSprite
ShapeSprite
Sprite image = new ImageSprite(Resource1.largestar);
We have our sprite. Now we want it to do something. Whenever we want a sprite to do something, we need an Effect. For this example, we want the image to spin in the centre of the list, and to fade out while it does so. We add the Effects to the Sprite, saying when the effect should start and how long it will last.
image.Add(0, 2000, Effects.Rotate(0, 360 * 2.0f)); image.Add(1000, 1000, Effects.Fade(1.0f, 0.0f));
This says, during the first 2000 milliseconds after the sprite begins in the animation, the image should spin completely twice. The second statement says that 1000 milliseconds after the sprite begins, the sprite should take 1000 milliseconds to gradually fade completely from sight.
Most Sprites have some sort of MoveEffect given to them to move them around. However, we want this sprite to just stay in the centre of the list, so we give it a FixedLocation:
MoveEffect
FixedLocation
image.FixedLocation = Locators.SpriteAligned(Corner.MiddleCenter);
This says the images MiddleCenter will always be aligned to the MiddleCenter of the animation (which in this case is on the whole of the list).
MiddleCenter
The final steps are to add the Sprite to the Animation:
animation.Add(0, image);
This says to begin running the image sprite 0 milliseconds after the animation starts (i.e. immediately). Often the sprites would not start until some time after the animation begins, but in this case, there's only one sprite so it may as well start immediately.
The animation is now fully configured and all that remains is to run it:
animation.Start();
All being well, this should produce an animation on the ObjectListView that looks something like this:
Again, CodeProject doesn't support animations within a page, so click here to see the actual animation.
In eight lines of code, you've put a spinning, fading star (a la Picasa) onto your otherwise static ListView.
Reflection is used in a couple of ways: to get data from the model objects, to put data into cell editors, and to put data into the model objects.
Getting data from the model object uses reflection to dynamically invoke a method, property or field by its name.
protected object GetAspectByName(object rowObject) { if (String.IsNullOrEmpty(this.aspectName)) return null; BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.GetProperty | BindingFlags.GetField; try { return rowObject.GetType().InvokeMember(this.aspectName, flags, null, rowObject, null); } catch (System.MissingMethodException) { return String.Format("Missing method: {0}", this.aspectName); } }
Things to note in this code:
null
BindingFlag
public
Static
protected
private
InvokeMember()
MissingMethodException
Reflection is easier to code, but you pay the penalty in speed. On my machine, reflection is 5-10x slower than using delegates. On a list of only 10-20 items, it doesn't matter. However, if your list has hundreds of items, it's worth installing AspectGetter delegates.
The real code is actually more complicated, since it supports using a dot notation to access sub-properties. It is valid to have several method or property names, joined by dots, as an aspect name. Each method or property name is de-referenced and the result of that de-referencing is used as the target for the next method or property. It's more intuitive to use than it is to explain " />
For example, Owner.Address.Postcode is a valid aspect name. This will fetch the Owner property from the initial model object and then ask that owner object for its Address. Then it will ask that address for its Postcode.
Owner
Address
Postcode
[As of v2.4, ObjectListView uses a more sophisticated scheme to access data through reflection. These improvements are housed in the Munger class. On average, these improvements are 3-5 times faster than standard reflection, which lessens (but does not completely remove) the value of writing custom AspectGetters.]
Munger
When we put a cell editor onto the screen, we need to get the value from the cell and somehow give it to the control. Unfortunately, there is no standard way for giving a Control a value. Some controls have a Value property, which is exactly what we want, but others do not. Where there is Value property, we want to use it, but where there isn't, the best we can do is use the Text method.
protected void SetControlValue(Control c, Object value, String stringValue) { // Look for a property called "Value". We have to look twice // since the first time we might get an ambiguous result PropertyInfo pinfo = null; try { pinfo = c.GetType().GetProperty("Value"); } catch (AmbiguousMatchException) { // The lowest level class of the control must have overridden // the "Value" property. // We now have to specifically look for only public instance properties // declared in the lowest level class. BindingFlags flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public; pinfo = c.GetType().GetProperty("Value", flags); } // If we found it, use it to assign a value, otherwise simply set the text if (pinfo == null) c.Text = stringValue; else { try { pinfo.SetValue(c, value, null); } catch (ArgumentException) { c.Text = stringValue; } } }
So, what's going on here?
Firstly, we use GetProperty to try and get information about the Value property on the control. We have to allow for ambiguous matches, which will occur if the controls immediate class has overridden the base class's Value property. In that case, we use some BindingFlags to say that we want the Value property that was declared in the lowest level class. To any language lawyers, yes, I know it's not foolproof, but it works in almost all cases.
GetProperty
BindingFlags
Once we have the property info, we can simply call the SetValue method. We have to catch the ArgumentException just in case the value can't be set.
SetValue
ArgumentException
If any of this has gone wrong, we simply use the Text method to put the value into the control and hope that it does what we want. s
ObjectListView was originally written to make a ListView easier to use, not to add swathes of new functionality. Initially, sub-item images were the only additional functionality (now, however, it does add swathes of new functionality). A plain vanilla ListView only supports images in the first column. ObjectListView doesn't have this restriction; any column can show images. To show images on sub-items, there are basically two strategies:
Owner drawing is a can of worms that I did not want to open, so I initially chose the second option. The ListView control in Windows has the ability to draw images against sub-items, but that functionality was not exposed in .NET. We can send messages to the underlying ListView control to make it show the images. Remember that these tricks rely on the underlying ListView control, so they may not work in future versions of Windows. It's certain that they will not work on non-Microsoft platforms. To make the ListView control draw sub-item images, we need to:
LVS_EX_SUBITEMIMAGES
Setting the extended style would be simple except that .NET doesn't expose the extended styles flag. So, we have to pull in the SendMessage() function and define the constants we want to use.
SendMessage()
[DllImport("user32.dll", CharSet=CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); private const int LVM_SETEXTENDEDLISTVIEWSTYLE = 0x1000 + 54; // LVM_FIRST+54 private const int LVS_EX_SUBITEMIMAGES = 0x0002;
Then, at some convenient point, you turn on the flag:
SendMessage(this.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES);
This would be enough, except that .NET Framework erases all unknown extended styles when an extended style is set. Examples are FullRowSelect and GridLines. So, the above code will have to be called after all other initialization is complete.
FullRowSelect
GridLines
Our second task is to tell the ListView control which sub-item will show which image. To do this, we need a new structure, LVITEM, and some more constants. We don't use most of the LVIF_ constants, but they're included for completeness.
LVITEM
LVIF_
private const int LVM_SETITEM = 0x1000 + 76; // LVM_FIRST + 76 private const int LVIF_TEXT = 0x0001; private const int LVIF_IMAGE = 0x0002; private const int LVIF_PARAM = 0x0004; private const int LVIF_STATE = 0x0008; private const int LVIF_INDENT = 0x0010; private const int LVIF_NORECOMPUTE = 0x0800; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] private struct LVITEM { public int mask; public int iItem; public int iSubItem; public int state; public int stateMask; [MarshalAs(UnmanagedType.LPTStr)] public string pszText; public int cchTextMax; public int iImage; public int lParam; // These are available in Common Controls >= 0x0300 public int iIndent; // These are available in Common Controls >= 0x056 public int iGroupId; public int cColumns; public IntPtr puColumns; };
We also need to import SendMessage a second time, but with a slightly different signature. We use the parameter EntryPoint to import a function using a name other than the C# function name.
SendMessage
EntryPoint
[DllImport("user32.dll", EntryPoint="SendMessage", CharSet=CharSet.Auto)] private static extern IntPtr SendMessageLVI(IntPtr hWnd, int msg, int wParam, ref LVITEM lvi);
Finally, we can set up the sub-item images using a method like this:
public void SetSubItemImage(int itemIndex, int subItemIndex, int imageIndex) { LVITEM lvItem = new LVITEM(); lvItem.mask = LVIF_IMAGE; lvItem.iItem = itemIndex; lvItem.iSubItem = subItemIndex; lvItem.iImage = imageIndex; SendMessageLVI(this.Handle, LVM_SETITEM, 0, ref lvItem); }
In the above member, itemIndex is the 0-based index of the row in question. subItemIndex is the 1-based index of the sub-item and imageIndex is the 0-based index into the image list associated with the listview.
itemIndex
subItemIndex
imageIndex
Once we have our nice new UI widget, we still have one more important step: make it work within the IDE. The whole point of this ListView is that it should make the programmer's life easier. That means it has to integrate well with the development environment, which drops us into the scary world of attributes and metadata.
One problem with figuring out how to integrate with the IDE is that it is not well-documented. That is, some pieces are documented, but it is usually not clear what we should do with those pieces. You might read that you can use EditorAttribute to control how a particular property is edited, but it is hard to see how to use that information to put the right sort of editors onto your custom DataSource and DataMember properties.
EditorAttribute
That is where the quasi-magical Lutz Roeder's .NET Reflector is so useful [this has been bought by RedGate, and is now only available commercially (they have a 14-day trial)]; not just the public ones — and all the methods of each class. It then reverse-engineers the source code for the methods. It's an amazing and amazingly useful piece of software. Using the Reflector, it turns out that the right incantation for our DataSource property is the relatively simple, if unintuitive:
[AttributeProvider(typeof(IListSource))] public Object DataSource { ... }
However, for the DataMember property, we need to invoke this spell:
[Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))] public string DataMember { ... }
This has to be considered at least non-obvious given that DataMemberListEditor is not even mentioned in the SDK documentation.
DataMemberListEditor
To limit the widths of columns, we have to find some way to intercept attempts to modify them. There are three UI mechanisms to change the width of a column:
Fortunately, all three of these mechanisms ultimately use the same HDN_ITEMCHANGING message. We only need to catch that message and everything should be fine. The first part of the solution requires the handling of WM_NOTIFY events, like this:
HDN_ITEMCHANGING
WM_NOTIFY
protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x4E: // WM_NOTIFY if (!this.HandleNotify(ref m)) base.WndProc(ref m); break; default: base.WndProc(ref m); break; } }
Then we can handle the HDN_ITEMCHANGING message. If the change would make the column wider or thinner than it should be, we simply veto the change by returning a result of 1.
1
private bool HandleNotify(ref Message m) { bool isMsgHandled = false; const int HDN_FIRST = (0 - 300); const int HDN_ITEMCHANGINGA = (HDN_FIRST - 0); const int HDN_ITEMCHANGINGW = (HDN_FIRST - 20); NMHDR nmhdr = (NMHDR)m.GetLParam(typeof(NMHDR)); if (nmhdr.code == HDN_ITEMCHANGINGW) { NMHEADER nmheader = (NMHEADER)m.GetLParam(typeof(NMHEADER)); if (nmheader.iItem >= 0 && nmheader.iItem < this.Columns.Count) { HDITEM hditem = (HDITEM)Marshal.PtrToStructure( nmheader.pHDITEM, typeof(HDITEM)); OLVColumn column = this.GetColumn(nmheader.iItem); if (IsOutsideOfBounds(hditem, column) { m.Result = (IntPtr)1; // prevent the change isMsgHandled = true; } } } return isMsgHandled; } private bool IsOutsideOfBounds(HDITEM hditem, OLVColumn col) { // Check the mask to see if the width field is valid, if ((hditem.mask & 1) != 1) return false; // Now check that the value is in range return (hditem.cxy < col.MinimumWidth || (col.MaximumWidth != -1 && hditem.cxy > col.MaximumWidth)); }
The solution does not appear very complicated. However, reality is rarely so simple. For example, those of you with some knowledge about the header control might be thinking, "Hey! What about HDN_TRACK and its friends? Why don't you do anything about them?"
HDN_TRACK
Well, according to KB article #183258, Microsoft states that when a header control has the HDS_FULLDRAG style, it will receive HDN_ITEMCHANGING messages rather than HDN_TRACK messages. Since version 4.71 of common controls, header controls always receive the HDS_FULLDRAG style. So, still it seems that we only have to handle the HDN_ITEMCHANGING message.
HDS_FULLDRAG
The trouble is that this is not always true. Under XP SP2 (at least), header controls with the HDS_FULLDRAG style do not always send HDN_ITEMCHANGING messages rather than HDN_TRACK messages. This may be why Microsoft has withdrawn that particular KB article. On some machines, header controls send HDN_ITEMCHANGING events as they should, but on others, the header controls send the old sequence of messages: HDN_BEGINTRACK, HDN_TRACK, HDN_ENDTRACK, HDN_ITEMCHANGING, HDN_ITEMCHANGED.
HDN_BEGINTRACK
HDN_ENDTRACK
HDN_ITEMCHANGED
After quite a bit of digging, the crucial setting seems to be the Explorer option "Show Window Contents While Dragging." In an example of "truly bizarre side effects," if this option is turned on, the header will send HDN_ITEMCHANGING messages instead of HDN_TRACK messages (as it should). However, if it is turned off, the header sends lots of HDN_TRACK messages and only one HDN_ITEMCHANGING message at the very end of the process.
Having two possible sequences of events complicates my simple plan. If the "Show Window Contents While Dragging" option is turned on, the current code works perfectly. If it is off, things are uglier.
On the whole, if we receive multiple HDN_TRACK messages and only one HDN_ITEMCHANGING message, it's harder to control the resizing process. The reason is that there is no way to cancel just one HDN_TRACK message. If we return a result of 1 from a HDN_TRACK message, we cancel the whole drag operation, not just that particular track event. From the user's point of view, it would appear that when they dragged the column to its minimum or maximum width, the drag operation would simply stop, even when they hadn't released the mouse. This is clearly not what we want.
[v2.0] As of v2.0, ObjectListView modifies the HDN_TRACK message itself, changing the size of the column in place, which is the best solution.
Just in case someone else runs into this problem, there is a strange bug in the ListView code. The effect of this bug is that in between a BeginUpdate() and EndUpdate() pair, iterating over the Items collection and calling Items.Clear() does not work reliably if the ListView handle has not been created. For example, if you call the following method before the handle for listView1 has been created, nothing will be written to the debugging output:
BeginUpdate()
EndUpdate()
Items.Clear()
listView1
private void InitListView1() { this.listView1.BeginUpdate(); this.listView1.Items.Add("first one"); this.listView1.Items.Add("second one"); foreach (ListViewItem lvi in this.listView1.Items) System.Diagnostics.Debug.WriteLine(lvi); this.listView1.EndUpdate(); }
If you remove the BeginUpdate() and EndUpdate() pair or call the method after the handle has been created, the method will work as expected.
The source of this bug is that, deep in the bowels of the ListView code, when BeginUpdate() is called, the ListView begins caching list updates. When EndUpdate() is called, the cache is flushed. However, GetEnumerator() does not flush the cache or take it into account. So, iterating over Items between calls to BeginUpdate() and EndUpdate() will only return the items that were present before BeginUpdate(). There are at least two easy ways around this bug:
GetEnumerator()
Items.Count
Thanks to ereigo for helping me track down this bug.
I like pictures. I think it's neat that in Explorer you can put a little graphic in the bottom right of the listview. I wanted to do the same thing with an ObjectListView. Surely, it can't be that difficult. But it was.
What did I specifically want from this feature? I wanted a background image on the ObjectListView. It had to stay fixed in place, not scrolling when the ListView scrolls. It had to work on XP and Vista. It had to be easy to custom, ideally just setting an image within the IDE. If the image could be positioned in whatever corner, or have a varying level of transparency, those would be bonuses. And obviously, I wanted it to work flawlessly — though I would be content with working spectacularly well.
The classic solution is to intercept the WM_ERASEBKGROUND message, erase the ClientRectangle, draw whatever you want, and the rest of the control then draws over what you've already drawn. Easy.
WM_ERASEBKGROUND
ClientRectangle
But it doesn't work. Actually, it works, so long as you don't double buffer the ListView. While the ListView is unbuffered, the image drawn in the WM_ERASEBKGROUND handler appears fine. But, when the control is double buffered, it doesn't work. When DoubleBuffered is set to true, it also sets the style AllPaintingInWmPaint style, which means: don't use WM_ERASEBKGROUND, the paint handler will do everything, including erase the background. So, for a double-buffered ListView (which is what I want), drawing in the WM_ERASEBKGROUND handler doesn't work.
DoubleBuffered
AllPaintingInWmPaint
The second try was to use LVM_SETBKIMAGE. This WinSDK message tells a ListView to draw an image under the control. Exactly what I wanted. But life is rarely that easy.
LVM_SETBKIMAGE
The first difficulty was actually making it work. TortoiseSVN sometimes has a listview background image, and Stefan had kindly documented some of his troubles in getting it to work. Using the information there, I managed to put an image under the control! Excellent... well not really. It did put an image under the ListView, but with a number of unpleasant side-effects:
LVBKIMAGE
xOffset
yOffset
LVBKIF_FLAG_ALPHABLEND
The show stopper was with Details view. Column 0 always erased the image. I could live with the other problems but what's the good of a underlay image when column 0 wipes it out? I checked to see if Stefan had found a solution for this one, but he hadn't.
Details
[May 2012 Update] Windows 7 has improve this situation somewhat. It doesn't have the same problems with column 0 or subitem images. Grid lines are still a problem, and obviously, it doesn't work in owner drawn mode (In owner drawn mode, each cell draws itself, including its background, which covers the background image). If you can live with these limitations, native watermarks are quite neat. They are true backgrounds, not translucent overlays like the OverlayImage uses. They also have the decided advantage over overlays in that they work correctly even in MDI applications.
ObjectListView now [v2.5.1] has built in support for native backgrounds:
// Set a watermark in the bottom right of the control this.olv.SetNativeBackgroundWatermark(Resource1.redback1); // Set the background image positioned 50% horizontally and 75% vertically this.olv.SetNativeBackgroundImage(Resource1.redback1, 50, 75)); // Set a tiled background to the control this.olv.SetNativeBackgroundTiledImage(Resource1.limeleaf);
I eventually decided on using the Layered Window API, which .NET exposes through the Opacity and TransparencyKey properties of Form.
Opacity
TransparencyKey
Form
The idea was to place a completely transparent form over the top of ObjectListView, and then draw onto that form (Mathieu Jacques did the same thing with his LoadingCurtain idea). From the user's point of view, the image appears to be draw onto the ObjectListView, but from the ObjectListView point of view, the overlays are not there.
This idea was a good one, but there were many other complications before it actually worked. See here for the overly-detailed version of the story.
But it did eventually work. So, as of v2.2, ObjectListViews support overlays, which are non-scrolling, transluscent images or text drawn over the top of the list contents.
The overlays scheme is extensible, but the two most common overlays are image overlays and text overlays.
ImageOverlay
These two overlays are so common that ObjectListView comes with one of each predefined: OverlayImage and OverlayText properties. These predefined overlays are exposed to the IDE, and can be configured directly from there. So, for the majority of cases, this is the only understanding of overlays that you will need.
OverlayImage
OverlayText
Despite my best efforts, this scheme does not work on MDI applications. Overlays do not obey the z-ordering that MDI forms obey. This means that overlays appear on top of all MDI forms. There is no solution to this problem. Just don't use overlays in MDI applications. Your only option is to use native background images (with their limitations).
I have written this control in two other languages — Smalltalk and Python — and it is always one of the most useful items in my toolbox. I have used it in every project I have had in those languages and I'm sure it is just as useful here. I hope that the code encourages more "slothfulness" and gives programmers time to improve some other parts of their project, thus encouraging hubris as well.
v2.6 will also move towards using IEnumerable whenever possible. SelectedObjects, CheckedObjects, AddObjects(), InsertObjects(), RefreshObjects(), RemoveObjects(), and CopyObjectsToClipboard() will all be changed to use IEnumerable. This is with a view to using LINQ in future versions.
AddObjects()
InsertObjects()
RefreshObjects()
RemoveObjects()
CopyObjectsToClipboard()
It will probably add a data bindable TreeListView.
v3.0 will be a big change. Until now, each version has strove to maintain backwards compatibility. v3.0 will not have this as a strict goal. It will be backwards compatible where possible, but will drop properties, events and methods where they do not fit within the new scheme. In particular, features that were a moment of design weakness (I'm looking at you AlwaysGroupByColumn and your friends) will disappear.
AlwaysGroupByColumn
There is no definite timetable for version 3.0.
Another hard drive crash and my last remaining XP machine is no more. I no longer have access to XP or even Vista – only Windows 7.
I may try to purchase a cheap laptop simply to run XP, but for the moment, I cannot test ObjectListView on anything other than Windows 7.
CellEditTabChangesRows
CellEditEnterChangesRows
Sortable
Groupable
Searchable
Hideable
VirtualListView
GetNextItem()
GetPreviousItem()
OLVListView
TreeRenderer
IRenderer
SelectObject()
SelectObjects()
SelectedObject
TextMatchFilter.MatchKind
InlineMenu
SubMenu
ModalDialog
ColumnSelectionForm
OLVColumn.AutoCompleteEditorMode
AutoCompleteEditor
ObjectListView.IncludeColumnHeadersInCopy
ObjectListView.Freezing
TreeListView.ExpandedObjects
Expanding
Expanded
Collapsing
Collapsed
ObjectListView.SubItemChecking
Equals()
==
UseTranslucentSelection
UseTranslucentHotItem
BorderDecoration
CopySelectionOnControlCUsesDragSource
Alt-[arrow]
CheckBoxes
SaveState()
RestoreState()
GroupByOrder
None
GroupWithItemCountSingularFormatOrDefault
ClearObjects()
ModelDropEventArgs.RefreshObjects()
TreeListViews
OLVColumn.HeaderTextAlign
EditingCellBorderDecoration
OLVColumn.Wrap
ObjectListView.SmoothingMode
BuildList(true)
CellEdit
NewValue
AllowExternal
RearrangableDropSink
ObjectListView.HeaderMaximumHeight
ChangeToFilteredColumns()
InvalidCast
Click
OLVColumn.ValueToString()
HeaderFont
HeaderForeColor
SelectAllOnControlA
CopySelectionOnControlC
Name
UseOverlays
F2
ShowHeaderInAllViews
This release focused on formatting — giving programmers more opportunity to play with the appearance of the ObjectListView .
Decorations allow you to put pretty images, text and effects over the top of your ObjectListView
Groups have been overhauled for this release. Groups under XP remain unchanged, but under Vista and Windows 7, many more formatting options are now available.
ObjectListViews can now have cells that are hyperlinks.
The font and text color of the ObjectListView header can now be changed. You can also word wrap the header text.
In previous version, RowFormatter was the approved way to change the formatting (font/text color/background color) of a row or cell. But it had some limitations:
RowFormatter
AlternateBackgroundColors
OLVListItem
To get around all these problems, there is now a FormatRow event. This is called after the OLVListItem has been added to the control. Plus it has a DisplayIndex property specifying exactly where the row appears in the list (this is correct even when showing groups).
There is also a FormatCell event. This allows the programmer to easily format just one cell.
By using compiler attributes, ObjectListViews can now be generated directly from model classes. [Thanks to John Kohler for this idea and the original implementation]
When running on Vista and later, virtual lists can now be grouped! FastObjectListView supports grouping out of the box. For your own VirtualObjectListView you must do some more work yourself.
[This was more of a technical challenge for myself than something I thought would be wildly useful. If you do actually use groups on virtual lists, please let me know]
ShowCommandMenuOnRightClick
HotItemChanged
Hot*
UseExplorerTheme
OLVColumn.AutoCompleteEditor
OlvHitTest()
HitTest()
OLVListItem.GetSubItemBounds()
TextAlign
LastSortColumn
PrimarySortColumn
LastSortOrder
PrimarySortOrder
OLVListItem.Bounds
IncrementalUpdate()
This is primarily a bug fix release.
CellClicked
CellOver
CellRightClicked
BuildList()
CalculateCellBounds()
The two big features in this version are drag and drop support and image overlays.
ObjectListViews now have sophisticated support for drag and drop operations. An ObjectListView can be made a source for drag operations by setting the DragSource property. Similarly, it can be made a sink for drop actions by setting the DropSink property. The dragging is based on the IDragSource interface, and the drop handling revolves around the IDropSink interface. SimpleDragSource and SimpleDropSink provide reasonable default implementations for these interfaces.
Since the whole goal of ObjectListView is to encourage slothfulness, for most simple cases, you can ignore these details and just set the IsSimpleDragSource and IsSimpleDropSink properties to true, and then listen for CanDrop and Dropped events.
Rearrangable lists are supported through the RearrangeableDropSink class.
RearrangeableDropSink
This version added the ability to draw translucent images and text over the top over the ObjectListView contents. These overlays do not scroll when the list contents scroll. These overlays works in all Views. You can set an overlay image within the IDE using the OverlayImage and OverlayText properties. The overlay design is extensible, and you can add arbitrary overlays through the AddOverlay() method.
Views
AddOverlay()
CellToolTipControl
HeaderToolTipControl
SelectedColumn
TintSortColumn
SelectedColumnTint
Scroll
In the same way that 2.0 overhauled the virtual list processing, this version completely reworks the owner drawn rendering process. However, this overhaul was done to be transparently backwards compatible.
The only breaking change is for owner drawn non-details views (which I doubt that anyone except me ever used). Previously, the renderer on column 0 was double tasked for both rendering cell 0 and for rendering the entire item in non-detail view. This second responsibility now belongs explicitly to the ItemRenderer property.
ItemRenderer
modelObject[this.AspectName]
DataRows
DataRowViews
EditorRegistry
TriStateCheckBoxes
UseCustomSelectionColors
UseHotItem
TreeListView.RevealAfterExpand
VirtualObjectListViews
ItemCheck
ItemChecked
ImagesRenderer
FlagsRenderer<T>
This version adds some small features and fixes some bugs in 2.0 release.
ObjectListView.EnsureGroupVisible()
TreeView.UseWaitCursorWhenExpanding
internal
ObjectListView.FinishCellEditing()
ObjectListView.PossibleFinishCellEditing()
ObjectListView.CancelCellEditing()
TreeRenderer.LinePen
HideSelection
Version 2.0 is a major change to ObjectListView.
ListViewPrinter's
Pens
Brushes
CellToolTipGetter
HeaderToolTipGetter
BeforeSorting
AfterSorting
TypedObjectListView.GenerateAspectGetters()
CheckedAspectName
IsSearchOnSortColumn
CheckStateGetter
CheckState
booleans
BooleanCheckStateGetter
BooleanCheckStatePutter
CustomSorter
FastObjectListDataSource
SortObjects()
HighlightForegroundColor
HighlightBackgroundColor
EnableCustomSelectionColors()
AlwaysGroupBySortOrder
CheckedObject
SORT_INDICATOR_UP_KEY
SORT_INDICATOR_DOWN_KEY
CopySelectionToClipboard().
MakeColumnSelectMenu().
AddObject(s)/RemoveObject(s)
Add/RemoveObject(s)
GetItem()
UserControl
GetAspectByName()
AddObject/AddObjects/RemoveObject/RemoveObjects
BusinessCardRenderer
RenderDelegate
boolean
void
RendererDelegate
TopItemIndex
ColumnRightClick
SelectedIndex
GetCheckedObject()
GetCheckedObjects()
GetItemAt()
Windows.Forms
FlagRenderer
OLVColumn.IsVisible
SelectColumnsOnRightClick
CopySelectionToClipboard()
Objects
SecondarySortColumn
SecondarySortOrder
RightToLeftLayout
SelectedItem
GetColumn()
DrawAlignedImage()
ListItem
UseAlternatingBackColors
ListView.View
Owner.Workgroup.Name
OLVColumn.MakeGroupies()
Freeze()/Unfreeze()/Frozen
SetObjects(null)
GetSelectedObjects()
if...else
This code is covered by GNU General Public License v3.
This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)
General News Suggestion Question Bug Answer Joke Rant Admin
Math Primers for Programmers