Strange CListBox Behavior
-
I have a bizarre problem with a dialog. I created a dialog to move items between two lists. The left list box has the pool of possible items that are unused on the left and the items being used on the right. In between the two are a bunch of buttons to move selected items left or right, or move the entire list from one box to the other. The boxes are defined as:
CListBox m\_lstLeft; CListBox m\_lstRight;
In the .RC file the boxes are defined as follows:
LISTBOX IDC_LSEL_LSTLEFT,7,25,115,186,LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
LISTBOX IDC_LSEL_LSTRIGHT,178,25,115,186,LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOPThe code for loading and handling the boxes is the same with just the names of the boxes changed. For example the move right button handling code extracts an item from the left box and puts in the the right and the move left button does the opposite. Moving an item from the left box to the right works as expected. The code to get the item from the left box is:
CArray aryListBoxSel; aryListBoxSel.SetSize(nCount); m\_lstLeft.GetSelItems(nCount, aryListBoxSel.GetData()); for(i=0; i
If idx 2, the third item in the list ends up in val as expected.
However, moving from right to left, using a mirror image of the same code:
idx = aryListBoxSel.GetAt(i); val = (float)m\_lstRight.GetItemData(idx);
val is always the last item in the list, no matter what idx is.
I compared the code and everything is a mirror image of the other. The two lists are defined the same way, loaded the same way, and accessed the same way.
What could possibly be wrong? As far as I can tell, I am setting up two identical list boxes and getting two different results.
-
I have a bizarre problem with a dialog. I created a dialog to move items between two lists. The left list box has the pool of possible items that are unused on the left and the items being used on the right. In between the two are a bunch of buttons to move selected items left or right, or move the entire list from one box to the other. The boxes are defined as:
CListBox m\_lstLeft; CListBox m\_lstRight;
In the .RC file the boxes are defined as follows:
LISTBOX IDC_LSEL_LSTLEFT,7,25,115,186,LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
LISTBOX IDC_LSEL_LSTRIGHT,178,25,115,186,LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOPThe code for loading and handling the boxes is the same with just the names of the boxes changed. For example the move right button handling code extracts an item from the left box and puts in the the right and the move left button does the opposite. Moving an item from the left box to the right works as expected. The code to get the item from the left box is:
CArray aryListBoxSel; aryListBoxSel.SetSize(nCount); m\_lstLeft.GetSelItems(nCount, aryListBoxSel.GetData()); for(i=0; i
If idx 2, the third item in the list ends up in val as expected.
However, moving from right to left, using a mirror image of the same code:
idx = aryListBoxSel.GetAt(i); val = (float)m\_lstRight.GetItemData(idx);
val is always the last item in the list, no matter what idx is.
I compared the code and everything is a mirror image of the other. The two lists are defined the same way, loaded the same way, and accessed the same way.
What could possibly be wrong? As far as I can tell, I am setting up two identical list boxes and getting two different results.
-
I have a bizarre problem with a dialog. I created a dialog to move items between two lists. The left list box has the pool of possible items that are unused on the left and the items being used on the right. In between the two are a bunch of buttons to move selected items left or right, or move the entire list from one box to the other. The boxes are defined as:
CListBox m\_lstLeft; CListBox m\_lstRight;
In the .RC file the boxes are defined as follows:
LISTBOX IDC_LSEL_LSTLEFT,7,25,115,186,LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
LISTBOX IDC_LSEL_LSTRIGHT,178,25,115,186,LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOPThe code for loading and handling the boxes is the same with just the names of the boxes changed. For example the move right button handling code extracts an item from the left box and puts in the the right and the move left button does the opposite. Moving an item from the left box to the right works as expected. The code to get the item from the left box is:
CArray aryListBoxSel; aryListBoxSel.SetSize(nCount); m\_lstLeft.GetSelItems(nCount, aryListBoxSel.GetData()); for(i=0; i
If idx 2, the third item in the list ends up in val as expected.
However, moving from right to left, using a mirror image of the same code:
idx = aryListBoxSel.GetAt(i); val = (float)m\_lstRight.GetItemData(idx);
val is always the last item in the list, no matter what idx is.
I compared the code and everything is a mirror image of the other. The two lists are defined the same way, loaded the same way, and accessed the same way.
What could possibly be wrong? As far as I can tell, I am setting up two identical list boxes and getting two different results.
If you always get the same data value regardless of the index, all items may have the same data value. So check the code portions that set the item data of the right list box.
-
I have edited your question so all the information is visble. In future please use <pre> tags around code blocks.
Thank you. I was just going to post a message that he should do that.
-
If you always get the same data value regardless of the index, all items may have the same data value. So check the code portions that set the item data of the right list box.
Sorry about the formatting, it was the first time I posted anything with code here. The value is not always the same, it's whatever is last on the list. If the right list has the following strings: "1" "2" "4" "7" "10" "11" Selecting "2" and clicking on the move left button will get the "11" and it will be moved to the left. If you then select the "2" again (or anything else on the list) and click on the move left button, the "10" will be selected and moved. What gets selected is whatever is last on the list. The code for moving from the left box to the right is identical except for the control name and selecting the "2" would actually fetch the "2" from index 1 and move it. The index fetched in both cases is correct. What isn't working is the code that gets the value for the index. I'm completely baffled. The two are coded the same.
-
Sorry about the formatting, it was the first time I posted anything with code here. The value is not always the same, it's whatever is last on the list. If the right list has the following strings: "1" "2" "4" "7" "10" "11" Selecting "2" and clicking on the move left button will get the "11" and it will be moved to the left. If you then select the "2" again (or anything else on the list) and click on the move left button, the "10" will be selected and moved. What gets selected is whatever is last on the list. The code for moving from the left box to the right is identical except for the control name and selecting the "2" would actually fetch the "2" from index 1 and move it. The index fetched in both cases is correct. What isn't working is the code that gets the value for the index. I'm completely baffled. The two are coded the same.
Why are you casting string values to floats in your code? You should use your debugger to step through the code and see exactly what values are being stored in your array (and why is it declared with two int fields?), and how they are matching the items in the lists.
-
Why are you casting string values to floats in your code? You should use your debugger to step through the code and see exactly what values are being stored in your array (and why is it declared with two int fields?), and how they are matching the items in the lists.
SetItemData allows you to associate a 32 bit value with the index and GetitemData retrieves that data. When I do the AddString to put the strings in the list box, if successful I load a float value associated with the string with SetItemData. When retrieved it has to be cast back to float because GetItemData returns a DWORD. In this particular dialog the data is stored as a float, but is always an integer. The program does this because the same data space can be used for float values too, but if you're in this dialog, the data will always be integers stored as floats.
-
Sorry about the formatting, it was the first time I posted anything with code here. The value is not always the same, it's whatever is last on the list. If the right list has the following strings: "1" "2" "4" "7" "10" "11" Selecting "2" and clicking on the move left button will get the "11" and it will be moved to the left. If you then select the "2" again (or anything else on the list) and click on the move left button, the "10" will be selected and moved. What gets selected is whatever is last on the list. The code for moving from the left box to the right is identical except for the control name and selecting the "2" would actually fetch the "2" from index 1 and move it. The index fetched in both cases is correct. What isn't working is the code that gets the value for the index. I'm completely baffled. The two are coded the same.
You may debug your application and inspect all list items (index, string, value); optionally also using TRACE statements. But you should also show the code used to add items to the right list box because the source may be there (I guess that it is empty upon program start). When the list box has sorting enabled, the order of items shown on screen does not correspond to the indexes. So when adding an item, it should look like:
int ndx = m_lstRight.AddString(strItem);
m_lstRight.SetItemData(ndx, val); -
You may debug your application and inspect all list items (index, string, value); optionally also using TRACE statements. But you should also show the code used to add items to the right list box because the source may be there (I guess that it is empty upon program start). When the list box has sorting enabled, the order of items shown on screen does not correspond to the indexes. So when adding an item, it should look like:
int ndx = m_lstRight.AddString(strItem);
m_lstRight.SetItemData(ndx, val);The box isn't sorted, but that's how I assign data to the list box:
int idx;
idx = m_lstRight.AddString(szStr);
if(idx = LB_ERR)
{
m_lstRight.SetItemData(idx, (DWORD)val);
}I looked at the data being assigned to the list box and everything was fine. I haven't figured out how to look at the data in a CListBox. I put a watch on it, but I didn't see where the data was stored when I inspected the control.
-
The box isn't sorted, but that's how I assign data to the list box:
int idx;
idx = m_lstRight.AddString(szStr);
if(idx = LB_ERR)
{
m_lstRight.SetItemData(idx, (DWORD)val);
}I looked at the data being assigned to the list box and everything was fine. I haven't figured out how to look at the data in a CListBox. I put a watch on it, but I didn't see where the data was stored when I inspected the control.
That will set
idx
toLB_ERR
. I don't know what happens with theSetItemData
call then but there are two options: It will fail or set the value of the last item in the list. It should be:if(idx != LB_ERR)
{
m_lstRight.SetItemData(idx, (DWORD)val);
}When
SetItemData
fails when passingLB_ERR
(which is most probable), you can remove theif
condition. [EDIT] Just a tip for such conditions: Exchange the expressions. If you would have usedif(LB_ERR = idx)
you would have got a compiler error and noticed your fault.
-
That will set
idx
toLB_ERR
. I don't know what happens with theSetItemData
call then but there are two options: It will fail or set the value of the last item in the list. It should be:if(idx != LB_ERR)
{
m_lstRight.SetItemData(idx, (DWORD)val);
}When
SetItemData
fails when passingLB_ERR
(which is most probable), you can remove theif
condition. [EDIT] Just a tip for such conditions: Exchange the expressions. If you would have usedif(LB_ERR = idx)
you would have got a compiler error and noticed your fault.
That's the problem! For some reason when setting the left box, the exclamation point was in there, but it was missing from the setting of the right box. That usually flags an warning on compile. I wonder why it wasn't. It appears SetItemData is doing something though. The value returned with GetItemData is always the last value on the list. The list redraws after a move, so all the items were probably getting set to the last value for some reason I don't understand. It works now. I think I need glasses... Thanks!
-
That's the problem! For some reason when setting the left box, the exclamation point was in there, but it was missing from the setting of the right box. That usually flags an warning on compile. I wonder why it wasn't. It appears SetItemData is doing something though. The value returned with GetItemData is always the last value on the list. The list redraws after a move, so all the items were probably getting set to the last value for some reason I don't understand. It works now. I think I need glasses... Thanks!
Fine to hear that the problem is solved. If you use the tip from my edited previous post you will avoid such errors in the future. I'm using that since some years and it probably saved me a lot of time. According to your description it seems that
SetItemData
is setting the value of the last item when passingLB_ERR
(like with other list box function when passing -1). -
That's the problem! For some reason when setting the left box, the exclamation point was in there, but it was missing from the setting of the right box. That usually flags an warning on compile. I wonder why it wasn't. It appears SetItemData is doing something though. The value returned with GetItemData is always the last value on the list. The list redraws after a move, so all the items were probably getting set to the last value for some reason I don't understand. It works now. I think I need glasses... Thanks!