c# winform ListBox 自定义元素变动事件



需求:需要监控listBox元素变动后 重新统计计算。

新建类MyListBox继承ListBox。

 public class MyListBox : ListBox
    {

        #region 定义消息常数
        /// <summary>【缓存变量】ListBox添加消息</summary>
        private const int LB_ADDSTRING = 0x180;
        /// <summary>【缓存变量】ListBox插入消息</summary>
        private const int LB_INSERTSTRING = 0x181;
        /// <summary>【缓存变量】ListBox移除消息</summary>
        private const int LB_DELETESTRING = 0x182;
        /// <summary>【缓存变量】ListBox清空消息</summary>
        ///private const int LB_CLEARALL = 0x184;
        #endregion

        #region 定义事件
        //public event EventHandler<ListItemChangedEventArgs> ItemAdded;
        //public event EventHandler<ListItemChangedEventArgs> ItemInserted;
        //public event EventHandler<ListItemChangedEventArgs> ItemRemoved;
        public event EventHandler<ListItemChangedEventArgs> ItemChanged;
        #endregion

        #region 唤起事件的过程
        protected void OnItemChanged(ListItemChangedEventArgs e)
        {
            if (ItemChanged == null)
                return;
            ItemChanged(this, e);
        }

        /*
        protected void OnItemInserted(ListItemChangedEventArgs e)
        {
            if (ItemInserted == null)
                return;
            ItemInserted(this, e);
        }
        protected void OnItemRemoved(ListItemChangedEventArgs e)
        {
            if (ItemRemoved == null)
                return;
            ItemRemoved(this, e);
        }*/

        #endregion

        #region 监控系统消息
        protected override void WndProc(ref Message m)
        {
            var itemIndex = 0;
            var itemValue = string.Empty;
            switch (m.Msg)
            {
                case LB_ADDSTRING:
                case LB_INSERTSTRING:
                case LB_DELETESTRING:
                //case LB_CLEARALL:
                    itemValue = Marshal.PtrToStringUni(m.LParam);
                    OnItemChanged(new ListItemChangedEventArgs(itemIndex, itemValue));
                    break;
                    /* 
                    itemIndex = (int)m.WParam;
                    itemValue = Marshal.PtrToStringUni(m.LParam);
                    OnItemInserted(new ListItemChangedEventArgs(itemIndex, itemValue));
                    break;
                
                    itemIndex = (int)m.WParam;
                    itemValue = Marshal.PtrToStringUni(m.LParam);
                    OnItemRemoved(new ListItemChangedEventArgs(itemIndex, itemValue));
                    break;*/
                default:
                    break;
            }
            base.WndProc(ref m);
        }

        public class ListItemChangedEventArgs
        {
            private int itemIndex;
            private string itemValue;

            public ListItemChangedEventArgs(int itemIndex, string itemValue)
            {
                this.itemIndex = itemIndex;
                this.itemValue = itemValue;
            }
        }
        #endregion



    }

生成后,拖拽使用和ListBox一样。用法:

listBox_saveData.ItemChanged += ListBox_saveData_ItemChanged;

逻辑代码:

 private void ListBox_saveData_ItemChanged(object sender, MyListBox.ListItemChangedEventArgs e)
{

   try
{//do sth...}
   catch{}
}