Getting selected element in List Box Control

  • Thread starter Thread starter Vijay
  • Start date Start date
V

Vijay

Hi,
Iam new to VC++6.0.
I want to display selected item of the ListBox in a MessageBox. I know for
experience ppl it must be a kids play but for me who had worked in C# and
VB, it seems to be little different.
Can anyone write couple to lines of code for this?
Moreover, If I want to insert any element at the mth place of nth col in
list box of type Report. can I achieve it.

Thanks in anticipation.

Vin
 
Vijay said:
Iam new to VC++6.0.

Glad to have you among us but ....

.... that tool is 6 or so years old.
I want to display selected item of the ListBox in a MessageBox.

Send the listbox a WM_GETCURSEL message. That will get you the index into
the list of the currently selected item. If the result is 0xFFFFFFFF (-1 as
a signed integer) then there is no selection. Then you send a LB_GETTEXTLEN
using the index of the item to get the length of the string. Then you insure
that you have a buffer at least one character larger (for the trailing 0
byte). Finally you send a LB_GETTEXT message to fetch the text into the
buffer.
Moreover, If I want to insert any element at the mth place of nth col in
list box of type Report. can I achieve it.

Check the docs for the LB_INSERTSTRING message.

Regards,
Will
 
Thanks for ur reply, But all the things jumped over my head. VC++ is really
a tough thing.
I have figured out one way to do this. It is as

LV_DISPINFO* pDispInfo = (LV_DISPINFO*) NMHDR;
int nPos = pDispInfo->item.iItem ;
POSITION Pos = m_List1.GetFirstSelectedItemPosition();
CString ListText = m_List1.GetItemText((int)Pos - 1, nPos);

I am able to fetch the fetch the selected Item in ListText . But Iam not
sure whether it is a gud process to do the things. If u hav a better one
don't hesitate to write it down.
Thanks
Vijay
 
Vijay said:
Thanks for ur reply, But all the things jumped over my head. VC++ is
really a tough thing.

I sketched a way of handling a list box control because it is what you
mentioned in your post.
I have figured out one way to do this. It is as

LV_DISPINFO* pDispInfo = (LV_DISPINFO*) NMHDR;
int nPos = pDispInfo->item.iItem ;
POSITION Pos = m_List1.GetFirstSelectedItemPosition();
CString ListText = m_List1.GetItemText((int)Pos - 1, nPos);

This looks to me like you may be using a list view control as a virtual
listbox. If so, my reply is useless. In any event, it looks like you are
using MFC in which case you should probably post again in one of the groups
deveoted to it.

Regards,
Will
 
Back
Top