Click here to Skip to main content
15,887,442 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have some items in list when i selected one item in list and clicked the up button continuosly still it reached top place and after its reched top place my up button should get disbaled.
but its not disbling and its deleting item when it recahes top place and after we clicked the up button.
could you please help me here

What I have tried:

C++
onupbuutonclicked(){

 int item2 = m_clist_ctl.GetNextItem(-1, LVNI_SELECTED);
        int columns = m_clist_ctl.GetHeaderCtrl()->GetItemCount();
        //Leaving out the order# column(0)
        for (int i = 1; i < columns; i++)
        {
            CString str1 = m_clist_ctl.GetItemText(item2 + 1, i);
            CString str2 = m_clist_ctl.GetItemText(item2, i);
            m_clist_ctl.SetItemText(item2 + 1, i, str2);
            m_clist_ctl.SetItemText(item2, i, str1);
          
        }
        m_clist_ctl.SetItemState(item2, ~LVNI_SELECTED, LVNI_SELECTED);
        m_clist_ctl.SetItemState(item2 + 1, LVNI_SELECTED, LVNI_SELECTED);

}

OnLvnItemchangedList(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<lpnmlistview>(pNMHDR);
    // TODO: Add your control notification handler code here
    int count = m_clist_ctl.GetItemCount();
    int row = m_clist_ctl.GetSelectionMark();
    int lastRow = count - 1;
    if ((0 == count) || (row < 0)) {
 GetDlgItem(IDC_BUTTON_UP)->EnableWindow(FALSE);

               }
   else if ((count >= 2) && (row > 0)) {
            GetDlgItem(IDC_BUTTON_UP)->EnableWindow(TRUE);
        }
        else {
            GetDlgItem(IDC_BUTTON_UP)->EnableWindow(FALSE);
        }

   }

could you please tell me if anything si wrong in my code
Posted
v3

1 solution

The reason is that OnLvnItemchangedList is not called when changing the selection. It is called when changing the content by calling SetItemText in onupbuutonclicked. But when that is processed the selection is still on the old position and the button is not disabled.

So you might disable the button at the end of onupbuutonclicked when item2 == 1 (but it looks like your posted code is for button down instead of button up because you are exchanging item2 and item2+1).

You may also use a single call to enable / disable the button in OnLvnItemchangedList:
// Enable button when current selection is not first row
// No need to check item count because that is always >= 2 when row >= 1
GetDlgItem(IDC_BUTTON_UP)->EnableWindow(row > 0);
 
Share this answer
 

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