热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UsingtheCCheckListBoxandgettingcheckstatenotification

转自:http:www.codeproject.comkbcomboboxCCheckListBoxUsage.aspxIntroductionIliketheCC

转自:http://www.codeproject.com/kb/combobox/CCheckListBoxUsage.aspx

 

Introduction

I like the CCheckListBox class provided with MFC, however, it�s use isn�t obvious and the wizard assistance stops at the CListBox class. I�ll describe how to insert it easily in your project. (There may be easier ways to do it, but this is how I do it and it works).

I�ll also show how you can add event notification so that you can find out when the check box state changes.

Creating the CCheckListBox member


  • Create a new MFC Application or Dialog Application.
  • In the resource editor, add a "regular" list box to the dialog.
    • Right click the listbox properties, then the Styles tab;
    • Ensure the Owner Draw = Fixed;
    • Ensure Has Strings = checked;
  • Holding the CTRL key, double click on the listbox in the resource dialog.

The wizard will only give you the option to create it as a CListBox , choose that, we will change it in code.

In the header code, change the wizard generated code from:

Collapse

//
Dialog Data




//
{{AFX_DATA(CCheckListBoxCBNDlg)




enum
{ IDD = IDD_CHECKLISTBOXCBN_DIALOG };

CListBox m_ctlCheckList;

//
}}AFX_DATA

to:

Collapse

//
Dialog Data




//
{{AFX_DATA(CCheckListBoxCBNDlg)




enum
{ IDD = IDD_CHECKLISTBOXCBN_DIALOG };

//
}}AFX_DATA




CCheckListBox m_ctlCheckList;

In the body, change the following generated code from:

Collapse

void
CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//
{{AFX_DATA_MAP(CCheckListBoxCBNDlg)




DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);

//
}}AFX_DATA_MAP




}

to:

Collapse

void
CCheckListBoxCBNDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//
{{AFX_DATA_MAP(CCheckListBoxCBNDlg)




//
}}AFX_DATA_MAP




DDX_Control(pDX, IDC_LIST1, m_ctlCheckList);

}

Adding items to the CCheckListBox

Now, you can add stuff to the checklist in your OnInitDialog member, like:

Collapse

m_ctlCheckList.ResetContent();

//
m_ctlCheckList.SetCheckStyle( BS_AUTO3STATE );




m_ctlCheckList.SetCheckStyle( BS_3STATE );

m_ctlCheckList.AddString("
Fumble"
);

m_ctlCheckList.SetCheck( 0
, 0
);

m_ctlCheckList.AddString("
Bumble"
);

m_ctlCheckList.SetCheck( 1
, 1
);

m_ctlCheckList.AddString("
Gumble"
);

m_ctlCheckList.SetCheck( 2
, 2
);

Note that the MSDN documentation is a little flimsy when it comes to the description of BS_AUTO3STATE and BS_3STATE . If you use BS_3STATE , then you will not get check box notifications and the states are locked (changeable in code only). If you use BS_AUTO3STATE , then you will get notifications of state changes, and the check boxes will manage themselves. You will just have to experiment with them to see which one gives you the effect you want.

Determining check box state changes

You can still use the wizard for the check list control you�ve created, but you�ll see that the list is limited to CListBox specific items:

I wanted a handler to know when a check box state changed (not a selection change). To accomplish this, manually add an event handler in the header as shown below. Note that if the user clicks on a check box, you will get two events for the click, first, OnCheckchangeList1 , followed by OnSelchangeList1 .

Caution: This is important if you depend on the current selection to change the checkbox state in a structure. I.e., the call to GetCurSel will be the new selection in the OnCheck call, even though OnSelchange hasn�t been called.

Collapse

//
{{AFX_MSG(CCheckListBoxCBNDlg)




virtual
BOOL OnInitDialog();

afx_msg void
OnSysCommand(UINT nID, LPARAM lParam);

afx_msg void
OnPaint();

afx_msg HCURSOR OnQueryDragIcon();

afx_msg void
OnSelchangeList1();

afx_msg void
OnCheckchangeList1();

//
}}AFX_MSG

In the body, add the event handler to the message map:

Collapse

BEGIN_MESSAGE_MAP(CCheckListBoxCBNDlg, CDialog)

//
{{AFX_MSG_MAP(CCheckListBoxCBNDlg)




ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)

//
}}AFX_MSG_MAP




ON_CLBN_CHKCHANGE(IDC_LIST1, OnCheckchangeList1)

END_MESSAGE_MAP()

And add your implementation of the handler.

Collapse

void
CCheckListBoxCBNDlg::OnCheckchangeList1()

{

//
TODO: Add your control notification handler code here




TRACE( "
CCheckListBoxCBNDlg::OnCheckchangeList1/n"
);

}

Conclusion

At this point you have a check list box that you can easily extend. Several other CodeProject articles show multi check list box classes and list view report views with check boxes. This is the simplest implementation of the MFC CCheckListBox .

Some people do not like the CCheckListBox because it leads to some ambiguity, but it really depends on the context it�s used in. For example, does checking the item turn the thing on or does the thing get enabled. When does it get turned on, when I check it or when I press OK/Apply in the dialog. Use this control with caution.


推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 本文介绍了一种在PHP中对二维数组根据某个字段进行排序的方法,以年龄字段为例,按照倒序的方式进行排序,并给出了具体的代码实现。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
author-avatar
kaxiaoliog_334
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有