herzl shemuelian
herzl shemuelian

Reputation: 3508

MFC limit selected item in ClistCtrl

Hi I use ClistCtrl that have 20 items and I want to limit selected item number. for example only 10 item can be selected. how i can do it? thanks for your help herzl.

Upvotes: 0

Views: 1694

Answers (3)

user2675121
user2675121

Reputation: 191

So I wrote this code. It should work. Just create an event handler for the list

void CDatenbankView::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    int SelctedItems;
    SelctedItems = 0;
    int Index;

    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);

    for (Index = 0; Index < m_List.GetItemCount(); ) //Check every Item
    {
        if (m_List.GetItemState (Index, LVIS_SELECTED) == LVIS_SELECTED) //Checks if it is selected
        {
            if (SelctedItems > 10) 
            {
                MessageBox (_T("Cant select more than 10 Items"));
                for (Index = 0; Index < m_List.GetItemCount(); )
                {
                    m_List.SetItemState (Index, ~LVIS_SELECTED, LVIS_SELECTED);
                    Index++;
                }
                break;
            }
            else
            {
                SelctedItems++;
            }
        }
        Index++;
    }
    *pResult = 0;
}

m_List is my control variable for the CListCtrl

Upvotes: 1

Marius Bancila
Marius Bancila

Reputation: 16338

There is no built-in functionality for such a feature. You'd have to write your our code for that. Maybe you can find another way to do it, like having a source list and a "selection list". You copy/move items from the first to the second, but you do not allow the users to put more than 10 items into the destination list.

Upvotes: 0

Gautam Jain
Gautam Jain

Reputation: 6849

You would have to handle the LVN_ODSTATECHANGED notification message and count the number of selected item each time the LVIS_SELECTED state changes

Thanks

Upvotes: 1

Related Questions