Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / XML

In-place Editing of ListView subitems

Rate me:
Please Sign up or sign in to vote.
4.91/5 (117 votes)
19 Oct 2004CPOL4 min read 613.3K   17.6K   197   131
An extended ListView to allow for in-place editing of subitems using arbitrary controls as editors

Image 1

Introduction

While talking with one of our customers, he wanted an additional feature in one of our programs to let him drop a number of files onto our application and then modify certain properties of the resulting documents in a list.

Showing these documents in a ListView can be done easily, but editing of single properties requires a little work (since built-in ListView only allows plain editing of a ListViewItem's text). Because I didn't find anything pre-built, I decided to write my own in-place editing for ListViews, so here it is...

How It Is Done

In fact, in-place editing in a ListView isn't too much magic, but there are a few places where the plain .NET Framework classes aren't sufficient, so I had to use a little Interop.

First, you have to have a control to perform the actual editing of the SubItem. Which control you use is (almost) completely up to you. TextBox, ComboBox or DateTimePicker works fine, for example. Since this control is used only when a SubItem has been clicked, it should be invisible in the beginning.

Then you have to find out which SubItem has been clicked. This part is quite straightforward, I've added a method GetSubItemAt() to my ListViewEx to make things a little easier.

A little twist comes from column reordering. Standard ListView allows you to rearrange its columns while in report view (AllowColumnReorder property). Unfortunately, there is no built-in way to find out the current order of your columns, so this is where Interop came in handy:

C#
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
private static extern IntPtr SendMessage(IntPtr hWnd, 
                        int msg, int len, ref int [] order);

// ListView messages
private const int LVM_FIRST = 0x1000;
private const int LVM_GETCOLUMNORDERARRAY = (LVM_FIRST + 59);

Using these declarations, you can use the LVM_GETCOLUMNORDERARRAY message to get the ListView's current column order.

The next step is to move the editor control in place and to make it visible. Once the actual editing is being performed, the user must be able to accept or reject any changes he makes, so there are a few events that have to be caught while editing.

Usually, a click outside the editor control accepts any changes made, as does the Return key. Pressing ESC while in in-place editing mode converts back to the original SubItem text.

Because the editor control actually is not part of the ListView, I also had to look for any action that might change the size or location of the editor control. This was done overriding WndProc:

C#
protected override void WndProc(ref Message msg)
{
    switch (msg.Msg)
    {
        // Look for WM_VSCROLL, WM_HSCROLL or WM_SIZE messages.
        case WM_VSCROLL:
        case WM_HSCROLL:
        case WM_SIZE:
            EndEditing(false);
            break;
        case WM_NOTIFY:
        // Look for WM_NOTIFY of events that might also change the
        // editor's position/size: Column reordering or resizing
        NMHDR h = (NMHDR)Marshal.PtrToStructure(msg.LParam, typeof(NMHDR));
        if (h.code == HDN_BEGINDRAG ||
            h.code == HDN_ITEMCHANGINGA ||
            h.code == HDN_ITEMCHANGINGW)
            EndEditing(false);
        break;
    }

    base.WndProc(ref msg);
}

Here, scrolling and resizing of the ListView are monitored as well as changes to the ListView's column headers. If one of these messages is received, the input focus is transferred back to the ListView, thus ending in-place editing.

How to Use ListViewEx for in-place Editing

There are two ways to perform in-place editing with ListViewEx. First, you can use the new SubItemClicked event together with GetSubItemBounds() to position your editor control by yourself, or you can use StartEditing(), which performs all required calculations and control positioning by itself.

So, usually you would start by adding a ListViewEx and at least one control used as a cell editor to your Form. Don't forget to make your cell editor control invisible! Then wire up an event handler for SubItemClicked and actually start editing:

C#
private void listViewEx1_SubItemClicked(object sender, 
                         ListViewEx.SubItemClickEventArgs e)
{
    // Here, I use a ComboBox (comboBox1) as a cell editor:
    listViewEx1.StartEditing(comboBox1, e.Item, e.SubItem);
}

That's it!

I've included a small sample application to show you how to use ListViewEx with several different cell editors. Feel free to use the control or the source to your heart's desire and have fun!

Additional Features

Your comments gave me some hints on missing features, so meanwhile I've added an additional property DoubleClickActivation so that you can decide if the ListViewEx should enter editing mode when you click on a subitem or if a double click is required.

Another point was adding two new events (SubItemBeginEditing and SubItemEndEditing) to give the caller the possibility to control what's displayed in the editing control and what gets put back into the ListViewSubItem.
Now you're able to add a password field as a cell editor and transfer the plain password to and from the edit control without having it shown in the listview. Take a look at the sample project to see how it's done.

History

  • 09.04.2004
    • Initial release
  • 19.04.2004
    • Update to account for editor control and ListViewEx not sharing the same parent (thanks Eric-Paul)
    • Fixed code has been uploaded
  • 19.10.2004
    • Reviewed the whole project
    • Fixed a few bugs
    • Added new features (DoubleClickActivation, new events, higher level of control over the editing process,...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) 4voice AG
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: In-plcae Editing && MDI - Forms Pin
mav.northwind19-Feb-06 20:15
mav.northwind19-Feb-06 20:15 
QuestionException in WndProc Pin
Hez17-Feb-06 0:13
Hez17-Feb-06 0:13 
AnswerRe: Exception in WndProc Pin
hansk8-Mar-06 11:08
hansk8-Mar-06 11:08 
GeneralButton in a ListView Pin
NOKIA631012-Feb-06 15:01
NOKIA631012-Feb-06 15:01 
GeneralRe: Button in a ListView Pin
mav.northwind18-Feb-06 20:49
mav.northwind18-Feb-06 20:49 
Questionhow to work with sorted rows? Pin
mimi yu9-Feb-06 11:33
mimi yu9-Feb-06 11:33 
Generalthankyou Pin
ndat5-Jan-06 13:57
ndat5-Jan-06 13:57 
GeneralRe: thankyou Pin
ndat5-Jan-06 17:09
ndat5-Jan-06 17:09 
QuestionI Can't download the example Pin
joctv6-Dec-05 0:05
joctv6-Dec-05 0:05 
AnswerRe: I Can't download the example Pin
mav.northwind6-Dec-05 7:42
mav.northwind6-Dec-05 7:42 
GeneralVB .Net version Pin
saberfoyle3-Nov-05 22:30
saberfoyle3-Nov-05 22:30 
GeneralRe: VB .Net version Pin
SoulFurnace15-Dec-06 10:38
SoulFurnace15-Dec-06 10:38 
GeneralRe: VB .Net version Pin
SoulFurnace18-Dec-06 13:53
SoulFurnace18-Dec-06 13:53 
GeneralListViewEx Pin
RichAhem1-Nov-05 11:22
RichAhem1-Nov-05 11:22 
GeneralYou're a lifesaver Pin
Mr.No14-Oct-05 22:45
Mr.No14-Oct-05 22:45 
GeneralRe: You're a lifesaver Pin
albert196920-Feb-06 2:09
albert196920-Feb-06 2:09 
GeneralRe: You're a lifesaver Pin
Mr.No20-Feb-06 8:06
Mr.No20-Feb-06 8:06 
GeneralRe: You're a lifesaver Pin
VBKarl28-Aug-06 12:05
VBKarl28-Aug-06 12:05 
GeneralAnother Edit Pin
dbrenth29-Jun-05 8:12
dbrenth29-Jun-05 8:12 
GeneralFocus / LostFocus Pin
Member 188178518-Apr-05 23:37
Member 188178518-Apr-05 23:37 
GeneralError Pin
juli802416-Apr-05 22:18
juli802416-Apr-05 22:18 
GeneralRe: Error Pin
mav.northwind17-Apr-05 20:16
mav.northwind17-Apr-05 20:16 
GeneralModification: go to edit mode like explorer does (slow double click) Pin
lustuyck25-Mar-05 4:49
lustuyck25-Mar-05 4:49 
GeneralAcceptButton Pin
slide_o_mix18-Mar-05 7:27
slide_o_mix18-Mar-05 7:27 
GeneralUsing VB.net Pin
RedDragon5013-Mar-05 10:33
RedDragon5013-Mar-05 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.