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;
}
}
解决方案 »
- c#怎样判断不同的数据库
- 为什么向datatable中插入数据,只显示最新的一行数据
- ListView排序的问题
- 文件目录操作的疑问
- C#安装包检测目标机上驱动程序的问题
- asp.net 连接sql
- Crystal Report 安装部署问题
- DataGridView添加TreeView控件,进行数据绑定,TreeView为列头
- FastReport3.2高手请帮忙
- 关于SHARPDEVELOP的问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- ——————————删除mdb文件问题,进来就有分————————
- foreach可以自定义循环次数吗(急)??
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);
}
}
}
我这里还是正确的。。你的代码。