Solved

CListCtrl - How to prevent an item from being deselected when user clicks elsewhere on the listbox?

Posted on 2004-09-14
8
2,253 Views
Last Modified: 2013-11-20
I have a CListCtrl object (listbox). I understand that to make an item selected/highlighted, I can use SetItemState(nItem, LVIS_FOCUSED|LVIS_SELECTED, LVIS_FOCUSED|LVIS_SELECTED). Subsequently, the selection/focus will follow according to the item that user has click.

But once the user clicks on elsewhere on the listbox (not on any item), the item gets deselected. How can I prevent the listbox from getting deselection? My listbox should always have one item selected. And if the user clicks elsewhere on the listbox, the item that is currently selected should not be deselected and shld stay selected/highlighted.

Experts, pls advise!
0
Comment
Question by:xiAoYJia
  • 3
  • 3
8 Comments
 
LVL 8

Accepted Solution

by:
VinExpert earned 75 total points
ID: 12062353
Hi,

Which one you are using? a list box or list control. Both are different controls!!!
Go to properties of that list box (list control) in the resource editor, then select the option "Show Selection Always".

That will always show the highlight.

Try it out.
VinExpert
0
 

Author Comment

by:xiAoYJia
ID: 12062435
Hi,

Sorry that I have confused u people... Im using a CListCtrl. The "Show Selection Always" option has alr been selected. I mean that when user clicks elsewhere on the ListCtrl. The currently selected item will be deselected and i do not want that to happen.
0
 
LVL 14

Expert Comment

by:wayside
ID: 12063888
The standard expected way to do multiple selection is to have the user press ctrl+click (make sure you've turned off the single selection property). If you do what you say, in order to unselect everything the user has to go in and click each selected item separately, this can be annoying.

If you really want to do this, one way is to keep a list of everything that has already been selected and programatically reselect them after every click, and if a user clicks something that is already selected remove it from the list.

You might be able to control the unselecting by handling the LVN_ITEMCHANGING message, but I've never played around with that.
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.

 
LVL 8

Expert Comment

by:VinExpert
ID: 12064152
Hi,

If that option is enabled, then the list control selected row shall become as gray when the focus moves to some other control. That gives the indication that, that row is selected. And thats the stadard behavior. It shall not stay as blue by default when focus is not there.

Are u getting at least gray selection or thats also not there?

VinExpert
0
 

Author Comment

by:xiAoYJia
ID: 12071619
Yes im already getting a gray selection on the item (set in my codings) when the program first run. But subsequently once i click elsewhere on the list ctrl (not on any item), the selection highlight on the item will be lost and the listCtrl will not have any item selected. Im not doing any multi-selection thing. So that one we can put aside. Im only concern with having one item selected at a time and at least one item should be selected at all times.

And one more qn, how do i set the CListCtrl to get the focus so that by default my selected item is higlhlighted in blue?
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 12074820
Hmmm, Why are u setting the selection using code? Try and remove that and let it be handled by CListCtrl only to set the selection when user clicks the on that row.

To set the focus to list control window u can use SetFocus() function with that list control object.

Try it out.
VinExpert
0
 

Author Comment

by:xiAoYJia
ID: 12080829
Well the problem is that if I didnt set any item to be selected using my codings, no item will be selected in the CListCtrl at inital startup. And as i mentioned before i need at least one item to be selected at all times. From the way u say, it seems like by default at least one item should be selected when the program is run? And by default, item should not be deselected for no reason. Jus that when it lose focus it will be gray. And when it gets the focus, the selection will be in blue right?

Well it jus doesnt work for me... I am also not sure why is this so. Anyw thanks... If u have no solution for me.. I will try to solve it slowly.

Btw now when i run my program, due to the selection set in the codings, the first item in CListCtrl will be selected in gray. So i wan to set it to get the focus so that by default it can be selected in blue. So i used m_PcList.SetFocus(); The CListCtrl still do not get the focus and the item selected is still gray color. Wat is the reason?
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
lucky13 challenge 11 154
sum28 challenge 31 115
haveThree challenge 22 121
Problem to App 4 97
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

839 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