Solved

CListCtrl - How to Highlight an Item progamatically

Posted on 2000-02-20
11
1,546 Views
Last Modified: 2013-11-20
I've got a CListCtrl (and assoicated ImageList) which works except for...

I want the user to always be able to see which item is selected -- when you pick on an item, the icon turns "blue-ish" indicating that it is selected.  How can I initially set an item as selected (highlighted) when my Dialog pops up?  Also, its possible to pick "in between" items in the list and
then end up in a state in which NO item is highlighted -- this is bad.  I want always one item hilighted, never none...

I've tried a bunch of stuff, and read thru past related postings here.  I can
SetItemState and SetFocus ans so on, and sure enough, I can set the selection status as I want, but what I cannot figure out how to do is to cause the selected item to "highlight" (turn blue-ish like when its been picked).

I sure wish it worked with SetCurSel(n) just like a CListbox....
0
Comment
Question by:tullhead
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 1

Expert Comment

by:jianliuer
ID: 2541042
Try SetItemState() function like this:
      ListCtrl.SetItemState( index, LVIS_SELECTED | LVIS_FOCUSED ,
      LVIS_SELECTED | LVIS_FOCUSED);
Good Luck!
      

0
 
LVL 3

Expert Comment

by:akalmani
ID: 2541111
Hi tullhead !!
   In the properties of Listctrl u set the style for as Show Selection Always
0
 

Author Comment

by:tullhead
ID: 2543085
I tried Jianliuer's suggestion but it
did not work (I aslo tried preceeding
it with a call to SetFocus, as another
posting had claimed that works).  

akalmani -- That property (show selection always) sounds just like what I need!  But it aso doesn't help.  I set it from the IDE -- I wonder if it coud somehow actually NOT be set in the .rc file?  

Any more ideas??
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 11

Expert Comment

by:mikeblas
ID: 2543313
"Show Selection Always" works fine.

If you think it's not set in the *.RC file, just look at the *.RC file and see if it's there or not!

..B ekiM
0
 

Author Comment

by:tullhead
ID: 2543791
Hmmm.....  this all seems simple enough and everyone says it should work.  Ok, lets get serious!  Here are bits of my code.  In this case, all I'm trying to do is to initially stick 2 items in the CListCtrl and seect& hilight the first one.  As stated above, my problem is that I can never get the image to "highlight".


I have the "LVS_SHOWSELALWAYS" style requested, as verified by this snipit from
my *.RC file:

    CONTROL         "List1",IDC_IMLC,"SysListView32",LVS_SINGLESEL |
                    LVS_SHOWSELALWAYS | WS_BORDER | WS_TABSTOP,122,35,90,270


In my OnInitDialog function for the dialog panel which contains the CListCtrl,
I do this to fill up the list and attempt to pre-select and highlight the first
item in the list:

BOOL CTagDialog::OnInitDialog()
{
      CDialog::OnInitDialog();

      // Create the ImageList...
        m_ImageList.Create(90,100,ILC_COLORDDB,3,3);

        // Associate it with the ListCtrl...
      m_imlc.SetImageList(&m_ImageList, LVSIL_NORMAL);

        // Add 2 items...
      Add_New.LoadString(IDS_LATERAL_DIGIT);
      AddtoImList(Add_New);
      Add_New.LoadString(IDS_ANT_POST_D);
        AddtoImList(Add_New);

        // Do SOMETHING to Select and highlight the first item...
        m_imlc.SetFocus();
      m_imlc.SetItemState(0,LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);

        return true;
}


Note, my Dialog has a CListCtrl and a CImageList in it...

class CTagDialog : public CDialog
{
....
      CListCtrl   m_imlc;
....
      CImageList  m_ImageList;
....
}


0
 

Author Comment

by:tullhead
ID: 2543799
Oh, I forgot that "AddtoImList" is a little function I wrote to associate the correct "thumbnail" with a given ID.

Maybe its something I do wrong in here:void CTagDialog::AddtoImList(CString imnm)
{
      CBitmap th;
      CBitmap* dum = NULL;

        CString tmp;
        tmp.LoadString(IDS_LATERAL_DIGIT);
      if (imnm == tmp) th.LoadBitmap(IDB_LED_THUMB);
        tmp.LoadString(IDS_ANT_POST_D);
      if (imnm == tmp) th.LoadBitmap(IDB_APD_THUMB);
        tmp.LoadString(IDS_SOLAR_P);
      if (imnm == tmp) th.LoadBitmap(IDB_SOP_THUMB);  
        tmp.LoadString(IDS_LAT_HOOF_P);
      if (imnm == tmp) th.LoadBitmap(IDB_LHP_THUMB);  

      int nimage = m_ImageList.Add(&th,dum);
      LV_ITEM it;
      it.mask = LVIF_TEXT | LVIF_IMAGE;
      it.pszText = imnm.GetBuffer(0);
      it.iSubItem = 0;
      it.iImage = nimage;
      m_imlc.InsertItem(&it);
}


0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 44 total points
ID: 2544640
here is what I do to ensure that my list control has the correct attribute (using your variable names)

m_imlc.ModifyStyle(0,LVS_SHOWSELALWAYS);

(just after the CDialog::OnInitDialog call)

Then, to get an initial selection:

m_imlc.SetItemState(0,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);

Actaully, I have my own clas derived from CListCtrl that modifies the style in the PreSubclassWindow function (also turns on full row selection, gridlines, stes up tooltip etc)
0
 

Author Comment

by:tullhead
ID: 2545032
The LVS_SHOWSELALWAYS is definately set, although just to be double-extra-sure, I added the statement suggested by RONSLOW to re-assert it.

After much fidding, I find that things get slightly better if I surround my SetItemState call with SetFocus calls
(one before, one after).  BUT, still can't find anyway to have the item intially hilighted when the panel pops up.  Only after I start picking on items in my panel does the hilight show up.

One thing I notice, which by itself is annoying:  When I pick on any control in my dialog (including, but not limited to the CListCtrl) the item in the ImageList de-hilights while I hold the mouse button down, and then when I release the mouse button, it re-highlights (due to code I wrote on the other controls to keep re-asserting the selection of the ImageList).  This is really stupid..... I guess picking on other controls causes the CCtrlList to lose focus and hence de-highlight -- so how do I maintain highlight even when focus is lost....?

I'll soon have to rip out the CCtrlList and go back to back to my lousy CListbox that I had before I began my upgrade.... arg....

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2548148
The whole idea of the LVSA_SHOWSELALWAYS is for it to highlight when the list control does NOT have focus.  If that is not working then you must be stuffing it up somewhere in your code.

I think the biggest clue yet is when you said there is 'code (you) wrote on the other controls to keep re-asserting the selection'.  That is probably what is causing problems.

There should be no need to keep selecting the item once it is selected.  Get rid of it.

How about you start with a clean dialog to test with that just has a list control and maybe an edit box on it.  Get it working there (should be trivial).  Then build from there until you find out what it is you are doing that is stopping the list control from working as it should.

0
 
LVL 1

Expert Comment

by:jianliuer
ID: 2548721
Hi,tullhead,
"...As stated above, my problem is that I can never get the image to "highlight". ..."

Do you mean the problem is that you can highlight only the text of the item?





0
 

Author Comment

by:tullhead
ID: 2549002
jianliuer --

No, neither the image nor the text hilight.  

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Different colored text in ComboBox without Subclassing 8 66
Perl Awk Need Help 3 129
Problem to event 3 104
How do I get Window Title of all opened process? 4 14
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 …
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…
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.

830 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