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;
                }
            }
        }

  • 相关阅读:
    js 实现页面局部(或图片)放大功能(vue)
    momentjs 使用总结
    VUX的使用方法(以弹出框为例)
    vue alert插件(标题为图片)(自写)
    vue 五星评价插件
    ES6 学习笔记(基础)
    SVG
    纯 CSS 实现实心三角形和空心三角形
    selectedIndex和onchange事件
    DOM文档获取和简介
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/1518530.html
Copyright © 2011-2022 走看看