热门标签 | 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.


推荐阅读
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了作者在开发过程中遇到的问题,即播放框架内容安全策略设置不起作用的错误。作者通过使用编译时依赖注入的方式解决了这个问题,并分享了解决方案。文章详细描述了问题的出现情况、错误输出内容以及解决方案的具体步骤。如果你也遇到了类似的问题,本文可能对你有一定的参考价值。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
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社区 版权所有