Created Button Click Event c#

c#, click

Solution

    public MainWindow()
    {
        // This button needs to exist on your form.
        myButton.Click += myButton_Click;
    }

    void myButton_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Message here");
        this.Close();
    }

Problem

I have made a button using ``` Button buttonOk = new Button(); ``` along with other code, how can I detect if the created button has been clicked? And make it that if clicked the Form will close?

Original source

Related problems