Solved

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

Posted on 2002-05-07
10
1,989 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
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!

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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 76
maven archtype selection in eclipse 1 59
FizzBuzz challenge 9 87
Problem to Office 1 62
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

731 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