Click to See Complete Forum and Search --> : Geting item from ListBox


marekzmyslowski
July 26th, 2005, 03:48 AM
Hi. I've got a little problem. I've got ListBox(created not using MFC). There are 3 columns - File name, Date, Coments. I want do do something like that - when I select a row and Click the button I will see a MessageBox with full file path. How to do this ? How can I know which file was selected ?

humptydumpty
July 26th, 2005, 04:00 AM
Hi. I've got a little problem. I've got ListBox(created not using MFC). There are 3 columns - File name, Date, Coments. I want do do something like that - when I select a row and Click the button I will see a MessageBox with full file path. How to do this ? How can I know which file was selected ?
Here is one code which is going to gave all the value of your all selected rows

UINT uSelectedCount = m_ListCtrl1.GetSelectedCount();
int nItem = -1;
for (UINT ICount=0;ICount <uSelectedCount;ICount++)
{
if(nItem == -1)
m_ListCtrl1.Update(nItem);
nItem = m_ListCtrl1.GetNextItem(nItem, LVNI_SELECTED);
{
//i am using CSimple Array for storing all the value of List Control here is MultimediaFile
MultimediaFile *pNewMultiMedia = new MultimediaFile;
m_ListCtrl9.GetItemText(nItem,0,pNewMultiMedia->bstrFileName);
m_ListCtrl9.GetItemText(nItem,1,pNewMultiMedia->bstrFileType);
m_ListCtrl9.GetItemText(nItem,2,pNewMultiMedia->bstrFilePath);
m_ListCtrl9.GetItemText(nItem,3,pNewMultiMedia->bstrOFilePath);
}
}

Ejaz
July 26th, 2005, 04:23 AM
Take a look at LB_GETCURSEL (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listboxes/listboxreference/listboxmessages/lb_getcursel.asp) and LB_GETTEXT (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listboxes/listboxreference/listboxmessages/lb_gettext.asp) messages.

marekzmyslowski
July 26th, 2005, 04:36 PM
Hi. It's not what I mean. Describing with more details.
List box:
----------------------------------------------
| File name | Size (kB) |
----------------------------------------------
| command.com | 25 |
| autoexec.bat | 20 |
| win.com | 120 |
----------------------------------------------
I also have a table looking like this
table[0] = "C:\"
table[1] = "C:\"
table[2] = "C:\Window"

In this example when I get selected row I will get it number. Then I can get a file path in simple way - number of row is a number of an element in table. But I can sort a listbox by size. It will sort only a list box not a table. How now can I get a full path ?

Ejaz
July 27th, 2005, 12:25 AM
Take a look at CListBox::SetItemData (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcemfc/htm/clistbox_40.asp) and CListBox::GetItemData (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_clistbox.3a3a.getitemdata.asp). You can set the index of the element in the array through SetItemData and then sort on whatever field you want, whenever you want to get the path, just use GetItemData(...) to reterive the element index and get it out of the array.

marekzmyslowski
July 27th, 2005, 03:41 AM
I have another question about this. How can I do this using ListView instead of ListBox ?

Ejaz
July 27th, 2005, 03:48 AM
I have another question about this. How can I do this using ListView instead of ListBox ?

Use CListView::GetListCtrl (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_clistview.3a3a.getlistctrl.asp) and rest of the steps are the same. :thumb:

marekzmyslowski
July 27th, 2005, 04:03 AM
But how to do this not using MFC class CListView ? ? ?

Hobson
July 27th, 2005, 04:19 AM
Use listview messages:
::SendMessage(m_hWnd, LVM_SETITEM, 0, (LPARAM)pItem);
::SendMessage(m_hWnd, LVM_GETITEM, 0, (LPARAM)pItem);

Usage samples can be found at MFC source code files :D
Powodzenia

Hob

humptydumpty
July 27th, 2005, 04:22 AM
But how to do this not using MFC class CListView ? ? ?

what you mean by not CListView.

First tell us what is your requirement.and in which way you want to do this.

second thing

use CListCtrl,or CListViewCtrl

or something else you want to do

marekzmyslowski
July 27th, 2005, 04:28 AM
I dont want to use CListX beceause i must compile my program not using MFC. Second thing I've got only HWND to that ListView.

Hobson
July 27th, 2005, 04:30 AM
Hwnd to ListView is enough to send message to it. Use LVM_SETITEM and LVM_GETITEM messages to associate data with CListView entries.

Edit:
To make things a bit easier, you can use ListView_GetItem and ListView_SetItem macros

Hob