代码之家  ›  专栏  ›  技术社区  ›  Meidan Alon

已重写CreateParams的ListBox不会引发项事件

  •  0
  • Meidan Alon  · 技术社区  · 17 年前

    我创建了一个自定义列表框,如中所示 here

    知道为什么吗? 谢谢

    1 回复  |  直到 9 年前
        1
  •  1
  •   Hans Passant    17 年前

    当我尝试此代码时,它工作得很好:

      public partial class Form1 : Form {
        MyListBox mList;
        public Form1() {
          InitializeComponent();
        }
    
        protected override void OnLoad(EventArgs e) {
          mList = new MyListBox(this);
          mList.Location = new Point(5, 10);
          mList.Size = new Size(50, this.ClientSize.Height + 50);
          for (int ix = 0; ix < 100; ++ix) mList.Items.Add(ix);
          mList.SelectedIndexChanged += new EventHandler(mList_SelectedIndexChanged);
        }
    
        void mList_SelectedIndexChanged(object sender, EventArgs e) {
          MessageBox.Show(mList.SelectedIndex.ToString());
        }
    
        protected override void Dispose(bool disposing) {
          // Moved from Designer.cs file
          if (disposing) mList.Dispose();
          if (disposing && (components != null)) {
            components.Dispose();
          }
          base.Dispose(disposing);
        }
    
      }