?
Solved

Deleting Listbox entries using the keyboard delete key

Posted on 2003-03-30
7
Medium Priority
?
341 Views
Last Modified: 2013-11-20
I have a listbox with several entries.  I want to allow the user to delete entries from the listbox by highlighting an item and then selecting the delete key (the actual delete key on the keyboard).  Any ideas on how this can be done?
0
Comment
Question by:bpwallac
[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
  • 5
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 total points
ID: 8233307
BOOL CSampleDlg::PreTranslateMessage(MSG* pMsg)
{
    if ( pMsg->message == WM_KEYDOWN  &&  pMsg->wParam == VK_DELETE )
    {
        if ( pMsg->hwnd = m_list_box.m_hWnd )
        {
            int n = m_list_box.GetCurSel();

            if ( n >= 0 )
            {
                m_list_box.DeleteString(n);

                n--;
                if ( n < 0 ) n = 0;
                m_list_box.SetCurSel(n);
            }
        }
    }

    return CDialog::PreTranslateMessage(pMsg);
}

This code assumes that your listbox is placed on dialog and you have class member
CLIstBox m_list_box;
0
 

Author Comment

by:bpwallac
ID: 8235936
Very cool...I am new to MFC and was unaware of the PreTranslateMessage function.

One question though:
Could you explain the purpose of the following if statement:
if ( pMsg->hwnd = m_list_box.m_hWnd )
0
 

Author Comment

by:bpwallac
ID: 8236013
A continuation of the above comment...

The reason why I am asking is because I have three listboxes within the same CDialog derived class.  I want to allow the user to delete entries in each listbox individually.  So, somehow I am going to have to detect which listbox the user has the dotted, rectangular selection box in.

Understanding the if statement is my first step in that direction...
0
Independent Software Vendors: 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!

 

Author Comment

by:bpwallac
ID: 8236197
A continuation of the above comment...

The reason why I am asking is because I have three listboxes within the same CDialog derived class.  I want to allow the user to delete entries in each listbox individually.  So, somehow I am going to have to detect which listbox the user has the dotted, rectangular selection box in.

Understanding the if statement is my first step in that direction...
0
 

Author Comment

by:bpwallac
ID: 8236519
A continuation of the above comment...

The reason why I am asking is because I have three listboxes within the same CDialog derived class.  I want to allow the user to delete entries in each listbox individually.  So, somehow I am going to have to detect which listbox the user has the dotted, rectangular selection box in.

Understanding the if statement is my first step in that direction...
0
 

Author Comment

by:bpwallac
ID: 8236790
A continuation of the above comment...

The reason why I am asking is because I have three listboxes within the same CDialog derived class.  I want to allow the user to delete entries in each listbox individually.  So, somehow I am going to have to detect which listbox the user has the dotted, rectangular selection box in.

Understanding the if statement is my first step in that direction...
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 8237026
pMsg->hwnd = m_list_box.m_hWnd
means that message is posted to this list box. You can use also the following condition:
if ( GetFocus() == &m_list_box )
    ...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

800 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