Thread: Listview label editing

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291

    Listview label editing

    Hello, I'm trying to use the LVS_EDITLABELS style to allow label editing on all the rows of a listview control; the problem is that only works for the first column althought I click over another column (the edit box is placed always over the first column of the selected row).

    I'm thinking on get the selected column by getting the mouse position over the listview and then create an edit control and place it over the selected column of the selected row to do an alternative label editing. Is that the way to do it? Or I'm missing something about the default listview label editing?

    Thank's in advance
    Niara

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    That's pretty much how you have to do it. You can hook the ListView's edit procedure on the LVN_BEGINLABELEDIT notification and move the editbox it automatically creates where you want it. Don't forget to enable FULLROWSELECT if going this route.

    Here's a sample that may or may not be helpful.

    (declare WNDPROC oldEditProcedure; globally)
    Code:
    					    case LVN_BEGINLABELEDIT:
    					    {
    					        hEditBox = (HWND)SendDlgItemMessage(hwnd, LST_DATA, LVM_GETEDITCONTROL, 0, 0);
                                LV_ITEM lvItem = ( (NMLVDISPINFO*)lParam )->item;
    					        DWORD dwPos = GetMessagePos();
    					        POINT pLoc = { GET_X_LPARAM( dwPos ), GET_Y_LPARAM( dwPos ) };
    					        ScreenToClient( hwnd, &pLoc );
    
    					        LVHITTESTINFO	lvHit;
    					        lvHit.pt.x = pLoc.x;
    					        lvHit.pt.y = pLoc.y;
    					        lvHit.iItem = lvItem.iItem;
    					        SendDlgItemMessage(hwnd, LST_DATA, LVM_SUBITEMHITTEST, 0, (LPARAM)&lvHit);
                                lvEditRect.top = lvHit.iSubItem;
                                lvEditRect.left = LVIR_LABEL;
                                SendDlgItemMessage(hwnd, LST_DATA, LVM_GETSUBITEMRECT, lvItem.iItem, (LPARAM)&lvEditRect);
                                oldEditProcedure = (WNDPROC)GetWindowLongPtr (hEditBox, GWLP_WNDPROC);			
                                SetWindowLongPtr (hEditBox, GWLP_WNDPROC, (LONG_PTR)newEditProcedure);
                                if (lvHit.iSubItem < 2) { //value/address
                                    SendMessage(hEditBox, EM_SETLIMITTEXT, 8, 0);
                                }
                                lvItem.iSubItem = lvHit.iSubItem;
                                lvItem.pszText=txtInput;
                                lvItem.cchTextMax=sizeof(txtInput);
                                SendDlgItemMessage(hwnd, LST_DATA, LVM_GETITEMTEXT, lvItem.iItem, (LPARAM)&lvItem);
                                SetWindowText(hEditBox,lvItem.pszText);
    					        break;
    					    }
    Code:
    /********************************
    Listview editbox handler
    *********************************/
    LRESULT CALLBACK newEditProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    //    char testchar[56];
     
    switch (message)
    {
        case WM_CHAR:
            {                            
                if ( (SendMessage(hwnd, EM_GETLIMITTEXT, 0, 0) == 8) &&  (FilterHexChar(wParam) == 0) ) {
                    wParam = 0;
                }
                break;
            }
    	case WM_WINDOWPOSCHANGING:
    	    {
     			WINDOWPOS* windowPos = (WINDOWPOS*)lParam;
    			windowPos->x = lvEditRect.left;
    			windowPos->y = lvEditRect.top - 2;
    			windowPos->cx = lvEditRect.right - lvEditRect.left;
    			return CallWindowProc (oldEditProcedure, hwnd, message, wParam, lParam);
    	   }
    
    	default:
    
    	if (oldEditProcedure) {
    	return CallWindowProc (oldEditProcedure, hwnd, message, wParam, lParam);
    	}
    	else {
    	return DefWindowProc (hwnd, message, wParam, lParam);
    	}
    }
    	return CallWindowProc (oldEditProcedure, hwnd, message, wParam, lParam);
    }

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hello Viper187, thank's for your time and the piece of code.
    Let me take a deeper look intto it and I'll say something.

    Thank's
    Niara

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hi, that works great!!!

    Only one thing:
    Code:
    //WM_NOTIFY->LVN_BEGINLABELEDIT rocessing:
    if (lvHit.iSubItem<2)
        {//value/address
        SendMessage(hEditBox,EM_SETLIMITTEXT,8,0);
        }
    
    //newEditProcedure proc
    case WM_CHAR:
        {                            
        if((SendMessage(hwnd,EM_GETLIMITTEXT,0,0)==8) && (FilterHexChar(wParam)==0))
            {
            wParam=0;
            }
        }
    break;
    Is that necessary? I suppose thats a sample of how to restrict data entering to the edit box on certain columns, am I right?

    Thank's
    Niara

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by Niara View Post
    Hi, that works great!!!

    Only one thing:
    Code:
    //WM_NOTIFY->LVN_BEGINLABELEDIT rocessing:
    if (lvHit.iSubItem<2)
        {//value/address
        SendMessage(hEditBox,EM_SETLIMITTEXT,8,0);
        }
    
    //newEditProcedure proc
    case WM_CHAR:
        {                            
        if((SendMessage(hwnd,EM_GETLIMITTEXT,0,0)==8) && (FilterHexChar(wParam)==0))
            {
            wParam=0;
            }
        }
    break;
    Is that necessary? I suppose thats a sample of how to restrict data entering to the edit box on certain columns, am I right?

    Thank's
    Niara
    Right. It's not necessary. I just restricted the user to typing hex characters in that column.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Ok, thank's. I can go on with the application now

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Label within a label?
    By Manaxter in forum C# Programming
    Replies: 0
    Last Post: 11-04-2006, 09:49 AM
  2. Troubles with ListView and Toolbar
    By cornholio in forum Windows Programming
    Replies: 8
    Last Post: 11-14-2005, 01:26 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. preventing editing of List View item's label
    By larry in forum Windows Programming
    Replies: 7
    Last Post: 02-24-2003, 08:25 PM
  5. Listview??
    By SuperNewbie in forum C# Programming
    Replies: 4
    Last Post: 02-13-2003, 03:34 AM