• 如何为标准的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;
                }
            }
        }

  • 相关阅读:
    docker常用命令
    2020/10/10,饮食男女-对教条主义的补充和现实的摸索
    2020/08/24,约束力
    2020/08/21,迷茫的时候就去工作
    2020/08/21,神秘和平易近人
    2020/08/21,圣人和教条
    2020/07/21,翡翠梦境
    2020/10/10,生活不是阶段式跳跃的,是螺旋式的。
    2020/07/23,再论point,way,moment,time
    2020/07/13,旅游的意义是什么
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/1518530.html
Copyright © 2020-2023  润新知