Solved

Difference between CListCtrl and CListBox Item changed message

Posted on 2008-10-13
7
1,620 Views
Last Modified: 2013-11-20
Hi,
I am really having trouble understanding why the List changed functions of listbox and listctrl behave differently.
In my application i have 2 controls, a listbox and a list control.
for OnLbnSelChange function of the list box, even if i click anywhere on the listbox, the GetCurSel() function always return the index of selected item, i.e. it keeps the item selected even if click on anywhere else on the list box.

For listctrl, if an item is selected and i click anywhere else on the list ctrl, the position of GetFirstSelectedItemPosition() is always returned null and the item is not highlighted anymore.

how can i have the same behaviour same as listbox with list control??
0
Comment
Question by:LearningCpp
  • 4
  • 2
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 22708993
>>how can i have the same behaviour same as listbox with list control??

Well a list box and a list control are VERY different controls.  A list control for example supports an ICON view totally unlike a list box which is just single rows.

>>For listctrl, if an item is selected and i click anywhere else on the list ctrl, the position of GetFirstSelectedItemPosition() is always returned null and the item is not highlighted anymore.

It sounds like you want to have the single selection option (see resource view / properties) turned off - the default behaviour is single selection.
0
 
LVL 19

Expert Comment

by:alb66
ID: 22709054
In the resource editor, you must set the property "Show Selection Always" to true
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 22709228
>>and i click anywhere else on the list ctrl
The 'Show Selection Always' is only required if you click on another control such as the list box but still want the selection on the list control to be displayed. It has nothing to do with selecting multiple items on a list control.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 22709255
CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
   TRACE0("No items were selected!\n");
else
{
   while (pos)
   {
      int nItem = pList->GetNextSelectedItem(pos);
      TRACE1("Item %d was selected!\n", nItem);
      // you could do your own processing on nItem here
   }
}
0
 

Author Comment

by:LearningCpp
ID: 22715682
Andy,
I have Always Show Selection = true
as well as
Single Selection = true in my resource editor.

I have a function on OnLvnItemchangedListCtrl.
The req is to enable a button if any item is selected in the listcontrol.
so i have
if(pos == NULL)
disableButton()
else
Enablebutton()
CButton* pBtn;
pBtn = (CButton*) GetDlgItem(IDC_BTN_EDIT);
GotoDlgCtrl(pBtn); //to set focus on the edit button, so that after i select an item and press "SPACE on keyboard" the edit button will be invoked without user actually using mouse to click the button.(setfocus does not work here)


0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 22718498
Please post the complete code for the OnLvnItemchangedListCtrl. function.  (The snippets you posted should be functional).

Have you checked it is actually being called with a breakpoint for instance?
0
 

Author Closing Comment

by:LearningCpp
ID: 31505721
I removed the while loop though, but when user clicks anywhere else then the focus is lost from listcontrol, strange, but will work for now
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

18 Experts available now in Live!

Get 1:1 Help Now