list box index

How do I get the index of the item that is selected in the list box?
mejaz01Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
MadYugoslavConnect With a Mentor Commented:
If there is just one selected item:
   with GetCurSel() You get the index of selected item in listbox.
If there are more selected items:
   with GetSelCount() You get the number of selected items and with GetSelItems() You get array with index of selected items.
0
 
Meir RivkinFull stack Software EngineerCommented:
here is an example:
     int cnt = m_srcList.GetSelCount();
     int *rgIndex = new int(cnt);
     m_srcList.GetSelItems(cnt, rgIndex);    

     
     CString str;

CStringArray strArry;
     for(int i=0; i<cnt; i++){
          m_srcList.GetText(rgIndex[i], str);
               strArry.Add(str);
     }

now strArray contain the selected items and rgIndex contain array of the indexes of the selected items

good luck
0
 
MadYugoslavCommented:
To sedqwick !
That solution will not work if in listbox is not allowed multiselect.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MadYugoslavCommented:
To mejaz01 !
In my comment words if there is just one selected item or more than one item means that your list box alow to select just one or more items (mean what style is checked: multiselect or single select).
0
 
Meir RivkinFull stack Software EngineerCommented:
sorry MadYugoslav, i was typing while your comment was posted.

0
 
mejaz01Author Commented:
I have a listbox that selects single item.  It was a nice solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.