Solved

How do I override arrow keys in CListCtrl?

Posted on 1997-12-29
2
390 Views
Last Modified: 2013-11-20
I'm using VC++ 5.0 and am stumped again.  

I have a DAOrecordview class linked to a dialog I constructed in the dialog editor.  Inside this dialog is a
0
Comment
Question by:dombmark
2 Comments
 
LVL 1

Accepted Solution

by:
richhxb earned 150 total points
ID: 1313195
What you want to do is to let the user navigate the list use
arrow keys and the content of the corresponding item become
available for editing in a separate part of the dialog. So
you have to create your own CListCtrlEx class derived from
CListCtrl, and override the CListCtrl::PreTranslateMessage
function like this:

BOOL CListCtrlEx::PreTranslateMessage(MSG* pMsg)
{
    // These codes were added to enable scrolling the list
    // with correct index.
      
    int nItem = -1;

    {
        //For UP and Dowm arrow keys.
        if ( ( ( (pMsg->message == WM_KEYDOWN) ||
            (pMsg->message == WM_SYSKEYDOWN) ) && 
            ( (pMsg->wParam == VK_UP) ||
            (pMsg->wParam == VK_DOWN) ) ) != 0)
        {
            while ((nItem = GetNextItem(nItem, LVNI_ALL)) != -1)
            {
                if (GetItemState(nItem, LVIS_FOCUSED))
                break;
            }
                  
            if (nItem != -1)
            {
                // Here you get the index number, and nItem is
                // zero based, so for the first item in the list,
                // nItem = 0;
                // Now you can add your own code for display its
                // content in other edit boxes.
                // For example, use GetItemText(nItem, 0) to get
                // the first column of the item.
            }
                  
            return TRUE;
        }
    }

    {
        //For LEFT and RIGHT arrow keys.
        if ( ( ( (pMsg->message == WM_KEYDOWN) ||
            (pMsg->message == WM_SYSKEYDOWN) ) && 
            ( (pMsg->wParam == VK_LEFT) ||
            (pMsg->wParam == VK_RIGHT) ) ) != 0)
        {
            while ((nItem = GetNextItem(nItem, LVNI_ALL)) != -1)
            {
                if (GetItemState(nItem, LVIS_FOCUSED))
                break;
            }

            if (nItem != -1)
            {
                if (pMsg->wParam == VK_LEFT)
                {
                    if(nItem > 0)
                        nItem--;
                    else
                        nItem = 0;

                    // Here you can highlighten the previous item
                    // using SetItemState, the index number is
                    // nItem. Code is omitted.
                }

                if (pMsg->wParam == VK_RIGHT)
                {
                    if(nItem < GetItemCount() - 1)
                        nItem++;
                    else
                        nItem = GetItemCount() - 1;

                    // Here you can highlighten the next item
                    // using SetItemState, the index number is
                    // nItem. Code is omitted.
                }
                  
                // Now you get the index number with correct
                // highlightening the corresponding item.
                // So you can add your own code for display
                // its content in other edit boxes.
            }
                  
            return TRUE;
        }
    }

    return CListCtrl::PreTranslateMessage(pMsg);
}

Although some codes are omitted here, but you can still
use this idea to implement it. And it should works well.



0
 

Author Comment

by:dombmark
ID: 1313196
This was indeed part of the solution I was missing.  Having implementing the above I was still left with the problem of communicating my overridden arrow key processing (part of CListCtrlEx) to my view.  It was more complicated also because I wanted CListCtrlEx to be accessible by more than one View.  

What I ended up doing was to get a pointer to the mainframe and then using that mainfrm pointer to GetActiveView.  I then did RUNTIME_CLASS processing to identify which view was currently active.  Once I had the active view I implemented a series of if, else if statements each one for the specific view I wanted to process.  This arrangement allowed me to do what I wanted to do.

Of particular help in this process was article ID 108587 from the Microsoft Knowledge Base (HOWTO: Get current CDocument or CView from anywhere)

Thanks for the help!  


0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 57
Process filename extension 3 184
Shell script to login to remote m/c and execute commands 10 78
x-bar in Google Sheets 2 53
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question