Solved

How to get index for selcted item in CListCtrl using arrow key

Posted on 2002-05-07
10
1,938 Views
Last Modified: 2013-11-20
I hvae currently implemented a list control that displays a textual description for each item when it is clicked by the mouse.
I would like this description to also be displayed when an item is selected using the arraow keys.

I have tried the following but this doesn't seem to work.

int nCurSel = m_ExceptionListCtrl.GetNextItem(-1 , LVNI_FOCUSED   | LVNI_SELECTED);

It always seems to hold the previous one that was selected and so the descriptions are wrong.

Any help would be greatly appreciated.
Thanks
0
Comment
Question by:kmcdonagh
  • 2
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:Yechezkel
ID: 6993698
Where are you trying this?
0
 

Author Comment

by:kmcdonagh
ID: 6993706
Inside GetSelectedItem
0
 
LVL 1

Expert Comment

by:Yechezkel
ID: 6993722
?

CListCtrl doesn't have a GetSelectedItem.

Do you mean your own GetSelectedItem function? That doesn't help me much. I assume your function is getting called from a message handler. Which message? Or if your function is called from somewhere else, how do you know when to call it?
0
 

Author Comment

by:kmcdonagh
ID: 6993729
The list control is within a CPropertyPage and the code submitted above is called within the OnKeydownMapList
in the property page.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 37

Expert Comment

by:zzynx
ID: 6993862
Hi,

I think it will work if you place your code in the OnItemChangedXXX() function (triggered when the user makes a new selection in the list control [LVN_ITEMCHANGED]).

Bye.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 6993963
2 kmcdonagh
>It always seems to hold the previous one that was selected
I think you call GetNextItem in the time when item state is not actualized. You can test it in addition for test each item state in the loop:

state = GetItemState(i, LVIS_SELECTED | LVIS_FOCUSED);

//index = GetHotItem();?

But better use WM_NOTIFY with LVN_ODSTATECHANGED like zzynx wrote.
0
 
LVL 8

Expert Comment

by:VinExpert
ID: 7012048
Hi,

I guess, it will be better idea to loop thru and get the selected item as the key pressed may be to go down or up in the list control. In that case, ur code looks like

If ur GetSelectedItem() is in some dialog/propertysheet class

int CMyDlg::GetSelectedItem()
{
     int nItemCount = m_ctrlList.GetItemCount();
     
     for(int nIndex = 0; nIndex < nItemCount; nIndex++)
     {
          if(m_ctrlList.GetItemState(nIndex, LVIS_SELECTED))
          {
               return nIndex;
          }
         
          return -1;
     }
}

If ur GetSelectedItem() is within the list control derived class
int CMyListCtrlClass::GetSelectedItem()
{
     int nItemCount = GetItemCount();
     
     for(int nIndex = 0; nIndex < nItemCount; nIndex++)
     {
          if(GetItemState(nIndex, LVIS_SELECTED))
          {
               return nIndex;
          }
         
          return -1;
     }
}

The return value gives u the selected item index.

Try it out.
VinExpert
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 7013032
Hi kmcdonagh,
Hi VinExpert,

Looping through all the items (yourself) is not needed.
Don't forget the existence of CListCtrl::GetFirstSelectedItemPosition()!

void CYourDialog::OnItemchangedList(NMHDR* , LRESULT*)
{
  POSITION pos = m_list.GetFirstSelectedItemPosition();
  int iNdex=m_list.GetNextSelectedItem(pos);
  // That's it!
  ...
}

or

int CMyListCtrlClass::GetSelectedItem()
{
  int iNdex=-1;
  POSITION pos = m_list.GetFirstSelectedItemPosition();
  if (pos) iNdex=m_list.GetNextSelectedItem(pos);
  return iNdex;
}

or when multiple selects are allowed:

void CYourDialog::OnItemchangedList(NMHDR*, LRESULT*)
{
  int iNdex;
  POSITION pos = m_list.GetFirstSelectedItemPosition();
  int iNdex=m_list.GetNextSelectedItem(pos);
  while (pos)
  {
    iNdex=m_list.GetNextSelectedItem(pos);
    // That's one of the selected ones
  }
  ...
}

Bye
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
how to monitor remote shell execution on linux 9 93
modThree challenge 4 64
matchUp  challenge 9 72
wordcount challenge 11 78
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

20 Experts available now in Live!

Get 1:1 Help Now