Implementing the On Item Checked Event for the TListView Control

The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer displays files and folders.

ViewStyle := Report; CheckBoxes := True;

The WindowProc is a special procedure every TControl uses to respond to messages sent to the control.

Here‘s how to get notified when an item in the list view is checked or un-checked:

  1. Drop a TListView (name "ListView1") on a Delphi form. Add some items to the list view.
  2. Set ViewStyle to vsReport,
  3. Set CheckBoxes to true,
  4. In the form‘s OnCreate event hijack the ListView1‘s WindowProc.
  5. If the message being processed in CN_Notify (Delphi extension to the WM_NOTIFY message) and if the notification message is "LVN_ITEMCHANGED",
  6. Read the tagNMLISTVIEW record to grab additional data.
  7. If this is a state change (LVIF_STATE) and if the state of an item changes (LVIS_STATEIMAGEMASK) grab the changed item, read it‘s Checked property.
uses CommCtrl;  

procedure TForm1.FormCreate(Sender: TObject) ;
begin
  OriginalListViewWindowProc := ListView1.WindowProc;
   ListView1.WindowProc := ListViewWindowProcEx;
  end;  

  procedure TForm1.ListViewWindowProcEx(var Message: TMessage) ;
  var   listItem : TListItem;
begin   if Message.Msg = CN_NOTIFY then
begin
  if PNMHdr(Message.LParam)^.Code = LVN_ITEMCHANGED then
    begin
    with PNMListView(Message.LParam)^ do
   begin
    if (uChanged and LVIF_STATE) <> 0 then
      begin
           if ((uNewState and LVIS_STATEIMAGEMASK) shr 12) <> ((uOldState and LVIS_STATEIMAGEMASK) shr 12) then
             begin
                  listItem := listView1.Items[iItem];
                  memo1.Lines.Add(Format(‘%s checked:%s‘, [listItem.Caption, BoolToStr(listItem.Checked, True)])) ;
             end;
              end;
              end;
                end;
                end;   //original ListView message handling   

                OriginalListViewWindowProc(Message) ;
                end;
                procedure TForm1.GetCheckedButtonClick(Sender: TObject) ;
                var   li : TListItem;
             begin
             memo1.Lines.Clear;   memo1.Lines.Add(‘Checked Items:‘) ;
             for li in listView1.Items do
             begin
               if li.Checked then
             begin
               memo1.Lines.Add(Format(‘%s %s %s‘, [li.Caption, li.SubItems[0], li.SubItems[1]])) ;
             end;
            end;
           end; 

Note: Reading the description of the tagNMLISTVIEW record in the Windows API help, reveals that "uChanged" field notifies that the item attributes have changed. This field is zero for notifications that do not use it. Otherwise, it can have the same values as the mask member of the LVITEM record. Bits 12 through 15 of the "state" member specify the state image index. To isolate these bits, use the LVIS_STATEIMAGEMASK.

时间: 2024-10-15 21:52:28

Implementing the On Item Checked Event for the TListView Control的相关文章

Atitit vod click event design flow &#160;视频点播系统点击事件文档

Atitit vod click event design flow  视频点播系统点击事件文档 重构规划1 Click cate1 Click  mov4 重构规划 事件注册,与事件分发管理器分开 Click cate Main.js line730 // -------------------------------------------- // UI interaction // -------------------------------------------- $(documen

Android DragAndDrop API 拖拽效果 交换ListView的Item值

前言 Android系统自API Level11开始添加了关于控件拖拽的相关API,可以方便的实现控件的一些拖拽效果,而且比自己用Touch事件写的效果更好.下面就来看下DragAndDrop吧. 使用Android的DragAndDrop框架,我们可以方便的在当前布局中用拖拽的形式实现两个View之间数据的互换.DragAndDrop框架包括一个拖拽事件的类,拖拽监听器,以及一些帮助方法和类. 尽管DragAndDrop主要是为了数据移动而设计,但是我们也可用他做别的UI处理.举个例子,我们可

滑动删除ListView的Item的效果

本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除的功能.在Android上面实现比较麻烦.本例子中不仅实现了左滑删除功能.还实现了左滑赞.左滑分享.左滑收藏等功能.当然大家也可以根据自己项目的需求来修改功能.QQ和微信也实现了相同的功能.大家可以看看.先上程序运行的效果 采用的恶事一个开源库swipemenulistview.jar 代码如下 布局 <com.baoyz.swipemenulistview.SwipeMenuListVi

SharePoint Workflow架构(二)Workflow 事件处理(Event Processing)

[译者按]Andy Li这篇文章,是我看过的最好的,最透彻的关于SharePoint Workflow架构的文章.通过阅读他的文章,我才清楚的了解了SharePoint Workflow的运作机制,并且在遇到问题的时候,知道如何下手查找问题.因此翻译过来,希望对Workflow的开发人员有帮助. 这篇博客是由Andy Li贡献的,他是SharePoint开发人员支持组的处理疑难问题的工程师.原文地址.这个关于Workflow的系列,是他贡献给社区的,帮助大家更好的理解Workflow运行时的内部

微信小程序修改radio和checked的默认样式和图标

wxml: <view class="body"> <view class="body-content"> 第1题:企业的价值观是 ? </view> <view class="label">单选</view> </view> <view class="options"> <radio-group class="radio-gr

C#学习日记24----事件(event)

事件为类和类的实例提供了向外界发送通知的能力,实现了对象与对象之间的通信,如果定义了一个事件成员,表示该类型具有 1.能够在事件中注册方法 (+=操作符实现). 2.能够在事件中注销方法(-=操作符实现). 3.当事件被触发时注册的方法会被通知(事件内部维护了一个注册方法列表).委托(Delegate)是事件(event)的载体,要定义事件就的要有委托.  有关委托的内容请点击 委托(De... www.mafengwo.cn/event/event.php?iid=4971258www.maf

embody the data item with the ability to control access to itself

Computer Science An Overview _J. Glenn Brookshear _11th Edition Such communication needs have long been a topic of study among computer scientists, and many newer programming languages reflect various approaches to thread interaction problems. As an

Edit Individual GridView Cells in ASP.NET

Edit individual GridView cells without putting the entire row into edit mode.Examples using the SqlDataSource and ObjectDataSource controls are included. Introduction The ASP.NET GridView allows for a row of data to be edited by setting the EditIndex

CheckboxTree

checkbox效果图: 准备以下文件结构的三个文件: 1.CheckboxTree.js define([ "dojo/_base/declare", "dijit/Tree", "dijit/form/CheckBox", "dijit/_WidgetsInTemplateMixin", "dojo/text!./templates/TreeNode.html" ], function(declare,