您的位置:首页 > 其它

动态生成按钮 和事件 反射

2008-11-03 18:12 323 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wnd
{
public partial class Form1 : Form
{

private Button[] n = new Button[100];
private int[] kn = new int[100];

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.panel1.Controls.Clear();
int a = 0;
int x = 0, y = 0;
for (a = 0; a < 20; a++)
{
n[a] = new Button();
//n[a].BackColor = Color.co;
n[a].FlatStyle = FlatStyle.Standard;

n[a].Width = panel1.Width / 2;
n[a].Left = x * n[a].Width;
n[a].Height = panel1.Height / 10;
n[a].Top = y * n[a].Height;
n[a].Name = "btn" + a;
n[a].Text = "btn" + a;
panel1.Controls.Add(n[a]);
panel1.Controls[a].MouseDown += new MouseEventHandler(this.ButtonArray_OnClick);

x += 1;
if (x >= 2)
{
x = 0;
y += 1;
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
//int a = 0;
//int x = 0, y = 0;
//for (a = 0; a <= 99; a++)
//{
// n[a] = new Button();
// n[a].BackColor = Color.White;
// n[a].FlatStyle = FlatStyle.Flat;
// n[a].Width = panel1.Width / 10;
// n[a].Left = x * n[a].Width;
// n[a].Height = panel1.Height / 10;
// n[a].Top = y * n[a].Height;
// n[a].Name = "b" + a;
// panel1.Controls.Add(n[a]);
// panel1.Controls[a].MouseDown += new MouseEventHandler(this.ButtonArray_OnClick);

// x += 1;
// if (x == 10)
// {
// x = 0;
// y += 1;
// }
//}

}

private void ButtonArray_OnClick(object sender, MouseEventArgs e)
{
//MouseEventArgs arg = (MouseEventArgs)e;
//Button b1 = (Button)sender;
//if (arg.Button == MouseButtons.Right)
// b1.BackColor = Color.White;
//else
//{
// //b1.BackColor =Color.White ;
//}
MessageBox.Show((sender as Button).Name);

}

}
}

object objForm = SpringContext.Context.GetObject("SL_CT_01_BF01");
MethodInfo method = objForm.GetType().GetMethod("ShowDialog", Type.EmptyTypes);
method.Invoke(objForm, null);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: