Solved

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

Posted on 2004-09-14
8
2,201 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
sameEnds challenge 3 107
abstract class with all non abstract mentods 6 72
zeroFront challenge 7 71
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

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

23 Experts available now in Live!

Get 1:1 Help Now