Using F2 as a hot key to Rename an Item in a ListCtrl or TreeCtrl

I had learned how to use ListCtrl and TreeCtrl using the examples given in CodeGuru. Soon I felt the need for a hotkey F2 to rename an Item (As in Internet Explorer), and add CUT, COPY, PASTE and Undo on the EditCtrl that appears. These functionality can be achieved by a small modification to the Article posted by Zafir Anjum.

Although I have taken great care while coding this use this code at your own risk.

Add the following code to the .cpp file of your CListCtrl or CListView

BEGIN_MESSAGE_MAP(CMyListView, CListView)
    ON_WM_KEYDOWN()
    ON_NOTIFY_REFLECT_EX( LVN_BEGINLABELEDIT, OnBeginLabelEdit )
    ON_NOTIFY_REFLECT_EX( LVN_ENDLABELEDIT, OnEndLabelEdit )
END_MESSAGE_MAP()

void CMyListView::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags )
{
    CListCtrl& oListCtrl = GetListCtrl( );
    switch( nChar )
    {
    case VK_F2:
        {
            // To Use F2 as hot Key to get EditCtrl on the ListView it must have 
            // the Style LVS_EDITLABELS
            ASSERT( oListCtrl.GetStyle() & LVS_EDITLABELS );

            // don't do an Edit Label when the multiple Items are selected
            if( oListCtrl.GetSelectedCount( ) == 1 )
            {
                UINT nListSelectedItem = GetSelectedItem();
                VERIFY( oListCtrl.EditLabel( nListSelectedItem ) != NULL );
            }
            else
                CListView::OnKeyDown( nChar, nRepCnt, nFlags );
        }
        break;

    default:
        CListView::OnKeyDown( nChar, nRepCnt, nFlags );
        break;
    }

}

// this Function Returns the first Selected Item In the List
UINT CMyListView::GetSelectedItem()
{
    CListCtrl& oListCtrl = GetListCtrl( );
    // this Function Valid Only when a Single Item Is Selected
    ASSERT( oListCtrl.GetSelectedCount( ) == 1 );

    UINT nNoOfItems = oListCtrl.GetItemCount( );

    for( UINT nListItem = 0; nListItem < nNoOfItems; nListItem++ )
        if( oListCtrl.GetItemState( nListItem, LVIS_SELECTED )  )
            break;

    ASSERT( nListItem < nNoOfItems );
    return nListItem;
}

Add this in the MessageMap section of CMyListView or CMyListCtrl file.
    afx_msg void OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags );

Add this Function as a private Member Fn to your CMyListView or CMyListCtrl .h file.

    UINT GetSelectedItem();

Download demo project - 20 KB



Comments

  • no longer getting error

    Posted by Legacy on 04/28/1999 12:00am

    Originally posted by: Edward Evans

    I had close msdev and the next time I started it it worked just fine. Don't know why it couldn't find it the first time. In other words I had started up MSDev.exe and checked the AddIns boxes for the 2 addins and then loaded a project into that instance and got the errors. After closing and opening a new instace it worked just fine. Thanks for your help and efforts.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • CentreCorp is a fully integrated and diversified property management and real estate service company, specializing in the "shopping center" segment, and is one of the premier retail service providers in North America. Company executives travel a great deal, carrying a number of traveling laptops with critical current business data, and no easy way to back up to the network outside the office. Read this case study to learn how CentreCorp implemented a suite of business continuity services that included …

Most Popular Programming Stories

More for Developers

RSS Feeds