动态生成按钮 和事件 反射
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);
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);
相关文章推荐
- javascript动态生成按钮并绑定点击事件
- 在一个消息响应函数中响应多个动态生成的按钮点击事件
- 在一个消息响应函数中响应多个动态生成的按钮点击事件
- Asp.net(C#)动态生成表格中的按钮和事件响应
- 动态生成按钮并绑定事件
- asp.net C#动态添加创建生成button按钮控件没有触发执行响应click绑定事件处理
- C#动态生成按钮及定义按钮事件的方法
- c#WINFORM中如何为动态生成的按钮加背景图片
- 用按钮触发Javascript动态生成一个表格的代码
- 动态生成的html 点击事件失效原因
- Java的反射的动态生成
- 动态生成的服务端控件,添加服务端的响应事件
- C++ Builder 实现动态生成窗口、控件,以及处理控件事件
- C#.net 动态生成的button按钮及回发处理的实现
- 开发中遇到的问题-动态创建按钮并且添加带有参数的点击事件
- updatepanel中动态生成组件及绑定事件问题的解决
- jQuery on()方法给动态生成的元素绑定事件----实战分析
- WPF动态添加按钮并相应事件。
- [jquery] 给动态生成的元素绑定事件 on方法
- Ext.Net动态创建按钮-动态添加事件