Reputation: 27
I'm doing a "list" of buttons, like a Menu in a Form, and I'm trying to do it out of a table in a database, I'm doing this like this:
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);
}
My problem is in the sb.Click += catalogo.Evento
line, how can I do the eventy dynamically
Upvotes: 0
Views: 285
Reputation: 125187
option 1
Create a SimpleButton_Click
method in your form
private void SimpleButton_Click(object sender, EventArgs e)
{
//using (SimpleButton)sender you can find which botton is clicked
}
then in your loop, assign that method to Click
event:
sb.Click += new System.EventHandler(this.SimpleButton_Click);
option 2
assign a delegate/lambda expression to event this way:
//instead of sender and e, usually you should use different names
//because usually you are running this code in an event handler
//that has sender and e parameters itself
sb.Click += (object senderObject, EventArgs eventArgs) =>
{
//using (SimpleButton)sender you can find which botton is clicked
};
Upvotes: 1
Reputation: 17271
Use a lambda / anonymous method
SimpleButton sb = new SimpleButton();
sb.Text = catalogo.Nombre;
sb.Click += (sender, evntArgs) => {
//some dynamic mouse click handler here.
};
Upvotes: 3