Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign click event

I have an array of button which is dynamically generated at run time. I have the function for button click in my code, but I can't find a way to set the button's click name in code. So,

what is the code equivalent for XAML:

<Button x:Name="btn1" Click="btn1_Click">

Or, what should I place for "????" in the following Code:

Button btn = new Button();
btn.Name = "btn1";
btn.???? = "btn1_Click";
like image 562
KMC Avatar asked Sep 09 '25 16:09

KMC


2 Answers

Button btn = new Button();
btn.Name = "btn1";
btn.Click += btn1_Click;

private void btn1_Click(object sender, RoutedEventArgs e)
{
    // do something
}
like image 195
Tom Dudfield Avatar answered Sep 12 '25 06:09

Tom Dudfield


The following should do the trick:

btn.Click += btn1_Click;
like image 33
Daniel Hilgarth Avatar answered Sep 12 '25 06:09

Daniel Hilgarth