Deleting Listbox entries using the keyboard delete key

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?
bpwallacAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AlexFMConnect With a Mentor Commented:
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
 
bpwallacAuthor Commented:
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
 
bpwallacAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
bpwallacAuthor Commented:
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
 
bpwallacAuthor Commented:
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
 
bpwallacAuthor Commented:
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
 
AlexFMCommented:
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
All Courses

From novice to tech pro — start learning today.