CListCtrl - How to Highlight an Item progamatically

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....
tullheadAsked:
Who is Participating?
 
RONSLOWConnect With a Mentor Commented:
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
 
jianliuerCommented:
Try SetItemState() function like this:
      ListCtrl.SetItemState( index, LVIS_SELECTED | LVIS_FOCUSED ,
      LVIS_SELECTED | LVIS_FOCUSED);
Good Luck!
      

0
 
akalmaniCommented:
Hi tullhead !!
   In the properties of Listctrl u set the style for as Show Selection Always
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
tullheadAuthor Commented:
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
 
mikeblasCommented:
"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
 
tullheadAuthor Commented:
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
 
tullheadAuthor Commented:
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
 
tullheadAuthor Commented:
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
 
RONSLOWCommented:
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
 
jianliuerCommented:
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
 
tullheadAuthor Commented:
jianliuer --

No, neither the image nor the text hilight.  

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.