?
Solved

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

Posted on 2002-05-07
10
Medium Priority
?
2,061 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
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 200 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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 …
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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