Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code below is snippet of a code in my application whose goal is to get item data from a listbox.

C++
for (int j = 0; j <; iSelected; j++)
    {
        long long llCustomerID = SendMessage
        (GetDlgItem(hDlg, IDC_LIST1), LB_GETITEMDATA, pSelected[j], 0);
        vTransactions.push_back(llCustomerID);
    }

The item data has been set in this manner:
C++
int iPos = SendMessage(GetDlgItem(hDlg, IDC_LIST1), LB_GETCURSEL, 0, 0);
SendMessage(GetDlgItem(hDlg, IDC_LIST1), LB_SETITEMDATA, iPos, (LPARAM)llCustomerID);

Memory was allocated to the smart pointer as shown below:
C++
unique_ptr<int []> pSelected = make_unique<int []>(iSelCount);

Where, iSelCount is the number of selections made in the listbox as returned by LB_GETSELCOUNT message.

The problem is that llCustomerID receives 1, instead of the database value of the transaction which should be 25. The numeral 1 received happens to be the ID of the first transanctions listed in the lisbox which was never selected.

Where exactly have I missed it?

The code below shows how values were assigned to pSelected:
C++
int iSelected = SendMessage(GetDlgItem(hDlg, IDC_LIST1), LB_GETSELITEMS, iSelCount, (LPARAM)pSelected.get());


What I have tried:

I have spent quality time trying to debug, but am yet to get any desired outcome.
Posted
Updated 6-Nov-23 1:05am
v4
Comments
k5054 5-Nov-23 11:42am    
How are you assigning values to pSelected? The documentation for SendMessage indicates that all paramaters are IN parameters. If you're not assigning a value to pSelected[n] then the default seems to be to initialize the array elements to zero.
Gbenbam 5-Nov-23 12:05pm    
I have added that information to the question.

1 solution

I just tried the same code and it works correctly. You need to use the debugger to check what is happening. It would also help if you put the code in order, and showed the actual values that it uses.
 
Share this answer
 
Comments
Gbenbam 5-Nov-23 12:03pm    
The code works, but it is not returning the assignd value.It is returning a differnt value.The assigned value is 25. It is returning 1. One(1) happn to be the value assignd to the first item listed int th list box. It seem to be returning the order in which each selected itme was selected in the listbox. For the first selected item, it returns 1.
Richard MacCutchan 5-Nov-23 12:23pm    
Well you must be doing something wrong that we cannot see. As I said above, I ran code similar to what you have shown and in each case it returns the correct value. So edit your question and show us the missing details, and not in random order like you have above.
Gbenbam 5-Nov-23 14:54pm    
Thanks so much for your input. It eventually turned out that you are right. What was causing the problem was a combobox selection notification handling function that updates the list box content. In the said function, the item data was set incorrectly. Once again, thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900