Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2440
  • Last Modified:

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

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
xiAoYJia
Asked:
xiAoYJia
  • 3
  • 3
1 Solution
 
Vinayak KumbarSr Program ManagerCommented:
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
 
xiAoYJiaAuthor Commented:
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
 
waysideCommented:
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
Industry Leaders: 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!

 
Vinayak KumbarSr Program ManagerCommented:
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
 
xiAoYJiaAuthor Commented:
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
 
Vinayak KumbarSr Program ManagerCommented:
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
 
xiAoYJiaAuthor Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now