嗨,这是我的第一篇文章,请多多包涵。;)
我有一个在事件发生时生成按钮的小类。我似乎无法实现的是向委托 mybutton_MouseClick 添加代码,该代码在单击时更改单个生成的按钮的颜色。
所以我正在寻找一些可以做的代码: mybutton.BackColor = Color.Red;
namespace test_addObject
{
public partial class Form1 : Form
{
ButtonCreation bc = new ButtonCreation();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bc.createButton();
}
}
public class ButtonCreation
{
Random rdm = new Random();
public void createButton()
{
Button mybutton = new Button();
Form1.ActiveForm.Controls.Add(mybutton);
mybutton.Location = new Point(
rdm.Next(Form1.ActiveForm.Width - mybutton.Width),
rdm.Next(Form1.ActiveForm.Height - mybutton.Height));
//mybutton.BackColor = Color.Red; //this will generate them red.
mybutton.MouseClick += new MouseEventHandler(mybutton_MouseClick);
}
void mybutton_MouseClick(object sender, MouseEventArgs e)
{
//some code here to change the generated button color when they are clicked individually.
//...
//...
}
}
}