然后我在整个界面就放一个按钮(button0),作为触发事件
然后动态地按要求生成多个按钮但是鼠标的右击事件我没法处理 private void button0_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("succ");
}
}
现在的问题是,动态生成的按钮右击后无法触发这个事件。只有button0这个最原始的按钮才能触发...
我哪里做错了吗?
然后动态地按要求生成多个按钮但是鼠标的右击事件我没法处理 private void button0_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("succ");
}
}
现在的问题是,动态生成的按钮右击后无法触发这个事件。只有button0这个最原始的按钮才能触发...
我哪里做错了吗?
btn.MouseDown += new System.EventHandler(this.button0_MouseDown);
我这样写,会报错,重载与委托不匹配怎么写呢?
btn.MouseDown += new MouseEventHandler(this.button0_MouseDown);
才对,谢谢楼上的诸位!
//给你些了个事例 添加一个button1创建动态控件
private void button1_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "动态添加的控件";
btn.Name = "btn";
btn.Left = 100;
btn.Top = 100;
btn.Visible = true;
btn.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btn_MouseDown);
this.Controls.Add(btn);
} private void btn_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(sender.ToString());
}