Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I override arrow keys in CListCtrl?

Posted on 1997-12-29
2
Medium Priority
?
453 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 450 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

824 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