Click here to Skip to main content
11,495,688 members (65,213 online)
Click here to Skip to main content

In-place editing of ListView subitems

, 19 Oct 2004 CPOL 385.4K 11.6K 190
Rate this:
Please Sign up or sign in to vote.
An extended ListView to allow for in-place editing of subitems using arbitrary controls as editors.

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:

[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:

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:

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)

Share

About the Author

mav.northwind
Software Developer (Senior) 4voice AG
Germany Germany
No Biography provided

Comments and Discussions

 
Questionuseful control Pin
Member 1143191412-Feb-15 22:52
memberMember 1143191412-Feb-15 22:52 
QuestionSubitemChanged EventHandler 29.09.2014 Pin
Member 1062093528-Sep-14 22:07
memberMember 1062093528-Sep-14 22:07 
QuestionChange BackColor of Textbox subitem Pin
Member 1062093516-Sep-14 23:05
memberMember 1062093516-Sep-14 23:05 
GeneralGreat Work Pin
mafaz32130-Apr-14 1:46
membermafaz32130-Apr-14 1:46 
QuestionScrolling Problem Pin
Amir Sacic2-Aug-12 10:58
memberAmir Sacic2-Aug-12 10:58 
Questionworks like a charm! great! Pin
velt_99118-Apr-12 23:16
membervelt_99118-Apr-12 23:16 
Questionselect item Pin
Janos01210-Feb-12 1:32
memberJanos01210-Feb-12 1:32 
QuestionEntire row selection is not working in .net 4.0 Pin
O B Kiran7-Feb-12 20:58
memberO B Kiran7-Feb-12 20:58 
QuestionHow to get the value of editable textBoxes of listviewEx ? Pin
saraMughal9-Dec-11 1:58
membersaraMughal9-Dec-11 1:58 
GeneralMy vote of 5 Pin
Thandermax13-Nov-11 5:17
memberThandermax13-Nov-11 5:17 
QuestionTHANKS Pin
Thandermax13-Nov-11 5:17
memberThandermax13-Nov-11 5:17 
BugDatetimepicker String was not recognised as a valid datetime [modified] Pin
Member 821449417-Oct-11 15:37
memberMember 821449417-Oct-11 15:37 
Questionhow to change SubItems Value in SubItemEndEditing? Pin
StehtimSchilf12-Oct-11 2:01
memberStehtimSchilf12-Oct-11 2:01 
GeneralMy vote of 5 Pin
JeffreyNguyen26-Jun-11 19:02
memberJeffreyNguyen26-Jun-11 19:02 
GeneralAdd Image On Subitem? Pin
Member 38926185-May-11 4:52
memberMember 38926185-May-11 4:52 
GeneralHighlight selection Pin
sathishraja3-May-11 22:35
membersathishraja3-May-11 22:35 
GeneralMy vote of 4 Pin
tangbin2006-Apr-11 20:08
membertangbin2006-Apr-11 20:08 
Questionhow can I save the edited values on list view? Pin
Sandhya Yamarthi25-Mar-11 8:45
memberSandhya Yamarthi25-Mar-11 8:45 
Questionhow to set items in the 1st and subitems in the 2nd column are not to be edited? Pin
Sandhya Yamarthi25-Mar-11 8:38
memberSandhya Yamarthi25-Mar-11 8:38 
AnswerRe: how to set items in the 1st and subitems in the 2nd column are not to be edited? Pin
Sandhya Yamarthi25-Mar-11 9:40
memberSandhya Yamarthi25-Mar-11 9:40 
GeneralMy vote of 5 Pin
brotherbamboo2-Mar-11 13:44
memberbrotherbamboo2-Mar-11 13:44 
GeneralSuggestion: SubItemAfterEditingEventHandler Pin
Member 403088927-Jan-11 5:29
memberMember 403088927-Jan-11 5:29 
QuestionSendMessage causes ListViewEX to fire SubItemEndEditing event again [modified] Pin
XtErMiNaToR10226-Jan-11 5:10
memberXtErMiNaToR10226-Jan-11 5:10 
GeneralVery good work! Pin
Member 403088925-Jan-11 4:11
memberMember 403088925-Jan-11 4:11 
GeneralGroups Pin
Jean-BA14-Oct-10 0:05
memberJean-BA14-Oct-10 0:05 
GeneralVertical scrolling not working with horizontal scrollbar Pin
Nick Barton9-Jul-10 2:33
memberNick Barton9-Jul-10 2:33 
GeneralComboBox unwanted behaviour on arrow down in list Pin
Member 235184319-Mar-10 4:58
memberMember 235184319-Mar-10 4:58 
GeneralRe: ComboBox unwanted behaviour on arrow down in list Pin
Member 235184319-Mar-10 5:22
memberMember 235184319-Mar-10 5:22 
GeneralCode snippet to make the SubItemEndEditingEventArgs.Cancel flag work Pin
Gabriel Bizzotto10-Feb-10 2:57
memberGabriel Bizzotto10-Feb-10 2:57 
GeneralThanks alot Pin
david Grice8-Feb-10 12:49
memberdavid Grice8-Feb-10 12:49 
GeneralCode snippet to deactivate right-click editing Pin
Gabriel Bizzotto3-Feb-10 10:11
memberGabriel Bizzotto3-Feb-10 10:11 
GeneralExactly what I needed, worked right away. Pin
Gabriel Bizzotto21-Jan-10 4:53
memberGabriel Bizzotto21-Jan-10 4:53 
GeneralNeed to ask how to use this Listview subitem editing in VB .NET 2003 Pin
Abhijeet Ghosh25-Nov-09 20:54
memberAbhijeet Ghosh25-Nov-09 20:54 
GeneralThis is great. Small bug found. Pin
dking5135-Nov-09 11:19
memberdking5135-Nov-09 11:19 
GeneralRe: This is great. Small bug found. Pin
Gabriel Bizzotto10-Feb-10 3:00
memberGabriel Bizzotto10-Feb-10 3:00 
Generalthanks,very useful Pin
niwazhaocheng13-Sep-09 0:27
memberniwazhaocheng13-Sep-09 0:27 
GeneralA bug found and the fix Pin
mailtorakib31-Aug-09 2:41
membermailtorakib31-Aug-09 2:41 
GeneralRe: A bug found and the fix Pin
Sandhya Yamarthi25-Mar-11 9:11
memberSandhya Yamarthi25-Mar-11 9:11 
GeneralSimply Awesome Pin
mailtorakib28-Aug-09 12:17
membermailtorakib28-Aug-09 12:17 
GeneralEdit ListView Pin
Gautamma9-Feb-09 23:46
memberGautamma9-Feb-09 23:46 
Generalvb.net Pin
bostjan678-Feb-09 7:58
memberbostjan678-Feb-09 7:58 
GeneralRe: vb.net Pin
_obi_10-Nov-11 3:50
member_obi_10-Nov-11 3:50 
GeneralDisable checkbox auto checked Pin
aarontham29-Jan-09 6:58
memberaarontham29-Jan-09 6:58 
GeneralArgumentOutOfRangeException Pin
BTH5053-Sep-08 4:55
memberBTH5053-Sep-08 4:55 
GeneralVery Nice! Pin
MoldyMagnet5-Jun-08 20:50
memberMoldyMagnet5-Jun-08 20:50 
GeneralRe: Very Nice! Pin
Portatofe2-Oct-08 7:10
memberPortatofe2-Oct-08 7:10 
GeneralBugfix for Numeric up/down control Pin
Tomas Brennan2-May-08 3:50
memberTomas Brennan2-May-08 3:50 
GeneralRe: Bugfix for Numeric up/down control Pin
Tomas Brennan2-May-08 6:38
memberTomas Brennan2-May-08 6:38 
GeneralExcellent Pin
titwan16-Apr-08 6:46
membertitwan16-Apr-08 6:46 
GenerallinkLable as item Pin
southsouth31-Mar-08 10:40
membersouthsouth31-Mar-08 10:40 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Oct 2004
Article Copyright 2004 by mav.northwind
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid