zoukankan      html  css  js  c++  java
  • 如何为标准的ListBox添加ItemClick事件

    今天在项目中遇到了一个小问题,需要给ListBox添加ItemClick事件,很简单:

     public class MyListBox:ListBox
        {
            private static readonly object EventItemClick = new object();
            public event EventHandler<ListBoxItemEventArgs> ItemClick
            {
                add
                {
                    Events.AddHandler(EventItemClick, value);
                }
                remove
                {
                    Events.RemoveHandler(EventItemClick, value);
                }
            }      
          
           
            protected virtual void OnItemClick(ListBoxItemEventArgs e)
            {
                EventHandler<ListBoxItemEventArgs> handler = (EventHandler<ListBoxItemEventArgs>)this.Events[EventItemClick];
                if (handler != null)
                {
                    handler(this, e);
                }
            }

            protected override void OnClick(EventArgs e)
            {
                base.OnClick(e);
                for (int i = 0; i < this.Items.Count; i++)
                {
                    bool flag = this.GetItemRectangle(i).Contains(this.PointToClient(Control.MousePosition));
                    if (flag)
                    {
                        ListBoxItemEventArgs args = new ListBoxItemEventArgs(i);
                        OnItemClick(args);
                        break;
                    }
                }
            }
        }

        public class ListBoxItemEventArgs : EventArgs
        {
            private int _listBoxItem;

            public ListBoxItemEventArgs(int listBoxItem)
            {
                _listBoxItem = listBoxItem;
            }

            public int ListBoxItem
            {
                get
                {
                    return _listBoxItem;
                }
            }
        }

  • 相关阅读:
    oracle笔记
    log4j配置
    前段页面性能标准
    递归多叉树遍历
    // 获取元素拒顶部高度
    window.parent
    webpack打包
    vue源码解析推荐文章
    在vue项目中。artTemplate引入失败问题,修改源码
    webpack打包css前缀自动取消,以及样式冲突问题
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/1518530.html
Copyright © 2011-2022 走看看