您的位置:首页 > 其它

如何让ListCtrl的CheckBox只有一个处于选中状态

2017-03-25 10:17 585 查看
1、要让ListCtrl的每一行的第一列为CheckBox,需要做如下设置:

[cpp] view plain copy print?



listCtrl.SetExtendedStyle(m_listCtrl.GetExtendedStyle() | LVS_EX_CHECKBOXES);


listCtrl.SetExtendedStyle(m_listCtrl.GetExtendedStyle() | LVS_EX_CHECKBOXES);


2、为了限制ListCtrl只能有一行的CheckBox处于选中状态,则需要响应ListCtrl的 LVN_ITEMCHANGED 消息:

[cpp] view plain copy print?



ON_NOTIFY(LVN_ITEMCHANGED, IDC_LC_CONFIG, OnLvnItemchangedLcConfig) void CRestorePage::OnLvnItemchangedLcConfig(NMHDR *pNMHDR, LRESULT *pResult) { LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 if (m_LcConfig.GetCheck(pNMLV->iItem)) UnCheckOtherItem(m_LcConfig, pNMLV->iItem); *pResult = 0; }


ON_NOTIFY(LVN_ITEMCHANGED, IDC_LC_CONFIG, OnLvnItemchangedLcConfig)

void CRestorePage::OnLvnItemchangedLcConfig(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
if (m_LcConfig.GetCheck(pNMLV->iItem))
UnCheckOtherItem(m_LcConfig, pNMLV->iItem);
*pResult = 0;
}


其中UnCheckOtherItem(…)的代码如下:

[cpp] view plain copy print?



void CRestorePage::UnCheckOtherItem(CListCtrl& listCtrl, int index) { for (int i = 0; i<listCtrl.GetItemCount(); ++i) { if (i == index) continue; listCtrl.SetCheck(i, FALSE); } }


void CRestorePage::UnCheckOtherItem(CListCtrl& listCtrl, int index)
{
for (int i = 0; i<listCtrl.GetItemCount(); ++i)
{
if (i == index)
continue;
listCtrl.SetCheck(i, FALSE);
}
}


转自:http://www.cnblogs.com/strinkbug/archive/2008/01/05/1027332.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: