Solved

How do I override arrow keys in CListCtrl?

Posted on 1997-12-29
2
379 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now