Nancy
Nancy

Reputation: 21

disable a button on multiple selection of items in CListCtrl in MFC

How to disable a toolbar button on multiple selection of items in CListCtrl in MFC. Current implementation is if no elements are there then the button is disabled. Now the functionality needs to extended if multiple items are selected then the button needs to be disabled.

Void  CMainFrame::OnUpdate( CCmdUI* pCmdUI) 
 {   
     if(I_count==0)//if no items are present
     {
     pCmdUI->Enable(false);
     return;
     }        
 }

kindly suggest how to disable the button on multiple selection

Upvotes: 0

Views: 460

Answers (1)

Andrew Komiagin
Andrew Komiagin

Reputation: 6556

Simply use: CListCtrl::GetSelectedCount() to retrieve the number of selected items in the list view control.

So your implementation is going to look like this:

void  CMainFrame::OnUpdate(CCmdUI* pCmdUI) 
 {   
     CMyListView* pView = (CMyListView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
     int nSel = pView->GetListCtrl().GetSelectedCount();
     if(nSel == 0 || nSel > 1)
         pCmdUI->Enable(FALSE);
     else
         pCmdUI->Enable(TRUE);
 }

Of course you should add some error handling to make sure that windows are initialized:

if (pWnd != NULL && pWnd->GetSafeHwnd() != NULL)
{
    // TODO: safe to call HWND methods
} 

For better design as Constantine Georgiou has suggested it would be much cleaner if you move all view related code to your view class including OnUpdateUI handlers.

Upvotes: 2

Related Questions