Solved

How do I override arrow keys in CListCtrl?

Posted on 1997-12-29
2
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 162
lucky13 challenge 11 167
Updating statistics with error notification email in SQL server 4 128
Problem to Office 1 67
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.

738 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