Solved

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

Posted on 2004-09-14
8
2,275 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
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!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prevent this page from creating additional dialogs. 3 712
wait notify demo infinite loop 3 135
ShiftLeft challenge 21 88
canBalance challenge 34 104
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

713 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