如何动态制作按钮?

时间:2015-09-11 01:04:57

标签: c# winforms devexpress simplebutton

我正在做一个按钮的“列表”,比如表单中的菜单,我正试图用数据库中的表来做,我这样做:

foreach (Catalogos catalogo in catalogos)
{
    SimpleButton sb = new SimpleButton();
    sb.Text = catalogo.Nombre;
    sb.Click += catalogo.Evento;
    LayoutControlItem item = new LayoutControlItem();
    item.TextVisible = false;
    item.Control = sb;
    lcg.Add(item);
 }

我的问题出在sb.Click += catalogo.Evento行,我该如何动态地进行事件

2 个答案:

答案 0 :(得分:3)

使用lambda / anonymous方法

SimpleButton sb = new SimpleButton();
sb.Text = catalogo.Nombre;
sb.Click += (sender, evntArgs) => {
    //some dynamic mouse click handler here.
};

答案 1 :(得分:1)

选项1

在表单中创建#define U16_SET_BIT(u, n) (u |= U16_CREATE_MASK(n)) #define U16_CREATE_MASK(n) (1U << (n)) #define U16 unsigned short int main(void) { .... U16 candidates[13]; U16_SET_BIT(candidates[1], 0); .... } 方法

SimpleButton_Click

然后在你的循环中,将该方法分配给private void SimpleButton_Click(object sender, EventArgs e) { //using (SimpleButton)sender you can find which botton is clicked } 事件:

Click

选项2

以这种方式为事件分配一个委托/ lambda表达式:

sb.Click += new System.EventHandler(this.SimpleButton_Click);