Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
This listbox is a control on another dialog.

ListBox Definition:
LISTBOX IDC_LNGNAMEBOX,205,21,190,131,WS_VSCROLL | WS_HSCROLL |
ES_AUTOHSCROLL | WS_TABSTOP | LBS_NOTIFY | LBS_NOINTEGRALHEIGHT | LBS_MULTIPLESEL

DataExchange:
CListBox m_LngNameList;
DDX_Control(pDX, IDC_LNGNAMEBOX, m_LngNameList);

MessageMap:
ON_LBN_SELCHANGE(IDC_LNGNAMEBOX, OnSelChangedLNGBox)
ON_LBN_DBLCLK(IDC_LNGNAMEBOX, OnMouseDBClick)

Scenario:
In a multiple selection listbox, when we click on any selected item, the selection goes of. I want to make the selection to stay always once the user clicks it (atleast any one item selected always).

Mysolution:
Whenever the selection changes if the item is already selected then select it again. I have done this is the "OnSelChangedLNGBOX" function.

Problem:
ON_LBN_SELCHANGE is called when we click an item and release the mouse (OnMouseUp). So the problem is when user clicks on item and holds it, the item is unselected and i am selecting back it again in OnSelChangedLNGBox. This is annoying, it looks like item is flickering, and also it doesn't seem to be consistent with windows. In windows if you open any folder with many documents if you click on an already selected document on mouse down the document still stays selected, but my item is unselected.

Question?
How can i solve this? I am trying to capture the PreTranslateMessage
if (pMsg->message == WM_LBUTTONDOWN)
{
    SelectedItem = m_LngNameList.GetAnchorIndex();
}

But here selected item is not the correct one, it is not the same as the one i am getting in ON_LBN_SELCHANGE. Help please?
Posted
Updated 14-Jun-11 10:03am
v2

1 solution

Have a look at this Codeproject article[^] discussing the different modes of selection; Single, Multiple and Extended. The article is old, but still valid.
 
Share this answer
 
Comments
amarasat 14-Jun-11 14:53pm    
i have already looked at this article, it has everything except LBS_WANTKEYBOARDINPUT
or VKeyToItem message examples. These are the thing i want exactly. Like i have already tried by adding LBS_WANTKEYBOARDINPUT to my ListBox styles and when i defined the function

int CGenerationDlg::VKeyToItem(UINT nKey, UINT nIndex) , i am getting error VKeyToItem is not a member of CGenerationDlg. Still searching how to declare that function.
amarasat 14-Jun-11 16:02pm    
I was wrong this is not what i want, i implemented LBS_WANTKEYBOARDINPUT, it only captures events on a list box item from keyboard, but not a mouse click event.

I still need to know how to capture the mouse click event on a listbox item, is so how to get the index of the item that is selected or about to be selected. suggestions please

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900