public partial class Form1 : Form
{
private int count = 0;
public Form1()
{
InitializeComponent();
} private void btn_addButtons_Click(object sender, EventArgs e)
{
count++;
int localY = this.btn_addButtons.Height * count;
int localX = 10 * count;
Button toAddButton = new Button();
toAddButton.Name = "Button" + count;
toAddButton.Text = "按钮" + count + "";
toAddButton.Location = new Point(localX, localY);
toAddButton.MouseEnter += new EventHandler(btn_MouseEnter);
toAddButton.MouseLeave += new EventHandler(btn_MouseLeave);
toAddButton.Click += new EventHandler(btn_Click);
this.Controls.Add(toAddButton);
} private void btn_Click(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
textBox1.Text = "你单击了" + currentButton.Text;
}
private void btn_MouseEnter(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Blue; } private void btn_MouseLeave(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
currentButton.BackColor = System.Windows.Forms.Control.DefaultBackColor;
} private void btn_Click(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
textBox1.Text = "你单击了" + currentButton.Text;
}
}
{
private int count = 0;
public Form1()
{
InitializeComponent();
} private void btn_addButtons_Click(object sender, EventArgs e)
{
count++;
int localY = this.btn_addButtons.Height * count;
int localX = 10 * count;
Button toAddButton = new Button();
toAddButton.Name = "Button" + count;
toAddButton.Text = "按钮" + count + "";
toAddButton.Location = new Point(localX, localY);
toAddButton.MouseEnter += new EventHandler(btn_MouseEnter);
toAddButton.MouseLeave += new EventHandler(btn_MouseLeave);
toAddButton.Click += new EventHandler(btn_Click);
this.Controls.Add(toAddButton);
} private void btn_Click(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
textBox1.Text = "你单击了" + currentButton.Text;
}
private void btn_MouseEnter(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Blue; } private void btn_MouseLeave(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
currentButton.BackColor = System.Windows.Forms.Control.DefaultBackColor;
} private void btn_Click(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
textBox1.Text = "你单击了" + currentButton.Text;
}
}
toAddButton.Text = "按钮" + count + "";貌似C#里int不能当直接当string用吧,改成下面:
toAddButton.Name = "Button" + count.Tostring();
toAddButton.Text = "按钮" + count.Tostring() + "";
bt.Text = "newBT";
bt.Location = new Point(100, 100);
bt.Visible = true;
this.Controls.Add(bt);vs2010测试通过。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int count = 0;
public Form1()
{
InitializeComponent();
}
//int count = 0;
private void button1_Click(object sender, EventArgs e)
{
count++;
int localY = this.button1.Height * count;
int localX = 10 * count;
Button toAddButton = new Button();
toAddButton.Name = "Button" + count;
toAddButton.Text = "按钮" + count + "";
toAddButton.Location = new Point(localX, localY);
toAddButton.MouseEnter += new EventHandler(btn_MouseEnter);
toAddButton.MouseLeave += new EventHandler(btn_MouseLeave);
toAddButton.Click += new EventHandler(btn_Click);
this.Controls.Add(toAddButton);
}
private void btn_MouseEnter(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Blue; } private void btn_MouseLeave(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
currentButton.BackColor = System.Windows.Forms.Control.DefaultBackColor;
} private void btn_Click(object sender, EventArgs e)
{
Button currentButton = (Button)sender;
MessageBox.Show("你单击了" + currentButton.Text);
}
}
}
我这里还是正确的。。你的代码。