Solved

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

Posted on 2002-05-07
10
1,967 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 93
bigDiff challenge 17 112
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 131
Capture logon name 13 75
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

832 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