Solved

How do I override arrow keys in CListCtrl?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
Add content to output file 4 62
Thin secure Windows 10 5 75
Not needed 13 94
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 …
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

14 Experts available now in Live!

Get 1:1 Help Now