How to add a Button click event with C#

207 Views Asked by At

I have some problems adding a click event to my project. The main logic is correct but I need some help with the click event. The project puts some buttons on a Canvas and I would like to add a click event on them.

I'm using the Kinect v2 and every time is found a Body, and every time is found a SpineMid joint, the project draws some buttons around the body using the shape of a regular polygon.

In my case the problem is that I cannot click the buttons because they're cleared in every frame.

I call the function InitializeButtons every time I found that joint (and so, I created new buttons with every frame and then cleared them) so it's basically impossible to click them, there's no time to view the content of the button changing: they're immediately replaced!

Here's the code where I call the function:

foreach (Body body in bodies){
   foreach (var item in body.Joints){
               JointType jointType = item.Key;
               Joint joint = item.Value;
               if (joint.JointType == JointType.SpineMid) {
                  InitializeButtons(100, 200, center, verticies, 300)
               }
   }
}

And here's the code of the InizializeButtons function:

 private void InitializeButtons(int height, int width, Point center, Point[] verticies, int radius)
    {
        Button test_button1 = new Button() { Background = Brushes.Aquamarine, Height = height, Width = width, Content = "I'm test button1!" };
        Button test_button2 = new Button() { Background = Brushes.Aqua, Height = height, Width = width, Content = "I'm test button2!" };
        Button test_button3 = new Button() { Background = Brushes.Beige, Height = height, Width = width, Content = "I'm test button3!" };
        Button test_button4 = new Button() { Background = Brushes.BurlyWood, Height = height, Width = width, Content = "I'm test button4!" };
        Button test_button5 = new Button() { Background = Brushes.Bisque, Height = height, Width = width, Content = "I'm test button5!" };

        test_button1.Click += test_button1_Click;

        canvas.Children.Add(test_button1);
        canvas.Children.Add(test_button2);
        canvas.Children.Add(test_button3);
        canvas.Children.Add(test_button4);
        canvas.Children.Add(test_button5);

        Canvas.SetLeft(test_button1, 0);
        Canvas.SetTop(test_button1, 0);

        Canvas.SetLeft(test_button2, 0);
        Canvas.SetTop(test_button2, 0);

        Canvas.SetLeft(test_button3, 0);
        Canvas.SetTop(test_button3, 0);

        Canvas.SetLeft(test_button4, 0);
        Canvas.SetTop(test_button4, 0);

        Canvas.SetLeft(test_button5, 0);
        Canvas.SetTop(test_button5, 0);

        buttons = new List<Button> { test_button1, test_button2, test_button3, test_button4, test_button5 };

        verticies = Polygon.CalculateVertices(buttons.Count, radius, 18, center);

        int i = 0;
        foreach (Button button in buttons)
        {
            Canvas.SetLeft(button, verticies[i].X);
            Canvas.SetTop(button, verticies[i].Y);
            i++;
        }

        verticies = null;
    }

    void test_button1_Click(object sender, RoutedEventArgs e)
    {
        Button clickedButton = (Button)sender;

        clickedButton.Content = "let's try it";          
    }

Is there any way I can go beyond that? Any help is really appreciate.

Thanks

0

There are 0 best solutions below