简体   繁体   English

禁用MFC中CListCtrl中的多项选择项上的按钮

[英]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. 如何在MFC中的CListCtrl中的多项选择上禁用工具栏按钮。 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 请建议如何禁用多选按钮

Simply use: CListCtrl::GetSelectedCount() to retrieve the number of selected items in the list view control. 只需使用: CListCtrl::GetSelectedCount()即可检索列表视图控件中选定项目的数量。

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: 当然,您应该添加一些错误处理以确保Windows已初始化:

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. 如Constantine Georgiou所建议的,为了获得更好的设计,如果将所有与视图相关的代码移动到包括OnUpdateUI处理程序的视图类中,将会更加OnUpdateUI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM