Solved

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

Posted on 2002-05-07
10
2,003 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
Technology Partners: 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!

 

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

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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