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

  • 相关阅读:
    MyEclipse数据库反向生成实体类
    Struts2部分标签
    如何使用 Jmeter 发送 Json 请求
    [转]Perfmon
    Jmeter 施压 SQL server数据库的时候,如何设置?
    Tomcat 安装之后,双击Tomcat.exe,无法运行成功,怎么办?
    Jmeter 在什么情况下定义多个thread group?
    Watir RAutomation VS AutoIt to deal with popup
    RAutomation 在 Watir中的使用
    AutoIt:如何处理应用程序端口被占用的情况
  • 原文地址:https://www.cnblogs.com/xixifusigao/p/1518530.html
Copyright © 2011-2022 走看看