当前位置:编程学习 > C#/ASP.NET >>

C# winform中动态创建的Listbox中怎么添加Click事件

C# winform中动态创建的Listbox中怎么添加Click事件

我这样写:
  ListBox lb = new ListBox();
    lb.Name = "mylb";
    lb.TabIndex = 100;
    lb.Location = tb.GetPositionFromCharIndex(tb.SelectionStart);
    lb.Left += 10;
    tb.Controls.Add(lb);
    lb.Click += new MouseEventHandler(lb_Click);这里出错

错误提示:
 无法将System.Windows.Forms.MouseEventHandler   转化为System.EventHandler
当我这样写时:
ListBox lb = new ListBox();
    lb.Name = "mylb";
    lb.TabIndex = 100;
    lb.Location = tb.GetPositionFromCharIndex(tb.SelectionStart);
    lb.Left += 10;
    tb.Controls.Add(lb);
    lb.Click += new EventHandler(lb_Click);这里出错
错误提示:
 lb.Click 的重载均与委托System.EventHandler不匹配

帮帮忙啊!!! --------------------编程问答--------------------             lb.Click +=   new System.EventHandler(this.listBox1_Click);

        private void listBox1_Click(object sender, EventArgs e)
        {
            Common.DisplayMsg(this.Text, "hello");
        }

       public static void DisplayMsg(string sCaption, string sMsg)
       {
           sMsg = sMsg.TrimEnd('!').TrimEnd('!') + " !";
           MessageBox.Show(sMsg, sCaption);
       } --------------------编程问答-------------------- http://p13.freep.cn/p.aspx?u=v20_p1_photo_1110050812101122_0.jpg --------------------编程问答-------------------- lb_Click的方法签名不对,返回值必须为void,参数为object和EventArgs
可以考虑用匿名方法和莱姆达表达式。
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,