Quantum Nova888
Quantum Nova888

Reputation: 5

how to subscribe multiple buttons to a event using for loop

When i tried to use a list of buttons and a for loop to subscribe selective buttons from the list to an event, the error 'An object reference is required for the non-static field, method, or property 'GamePage.ChessBlock_clicked(object, RoutedEventArgs)' appears and i have no idea how to resolve this, i just need to have each of the selective buttons to be able to access the "chessBlock_clicked" method if they are clicked. Help would be much appreciated, Thanks.

static List<Button> buttons = new List<Button>();
        public void ListofBlocks()
        {
            Button[] Blocks =
            {
                    Block1, Block2, Block3, Block4, Block5, Block6, Block7, Block8, Block9, Block10,
                    Block11, Block12, Block13, Block14, Block15, Block16, Block17, Block18, Block19, Block20,

                  // and more blocks
            };

            buttons.AddRange(Blocks);
        }


public static void Determine_turn()
        {
            for (int i = 0; i < buttons.Count; i++)
            {
                ChessPiece piece = (ChessPiece)buttons[i].Tag;
                if (Global.Turn == "white")
                {
                    if (piece == ChessPiece.WPawn || piece == ChessPiece.WKnight || piece == ChessPiece.WRook || piece == ChessPiece.WBishop || piece == ChessPiece.WQueen || piece == ChessPiece.WKing)
                    {
                        buttons[i].Click += ChessBlock_clicked;  // error occurs here
                    }
                }
                else if (Global.Turn == "black")
                {

                }
            }
        }


private void ChessBlock_clicked(object sender, RoutedEventArgs e)
        {

            if (sender is Button btn)
            {

                ChessPiece piece = (ChessPiece)btn.Tag;
                Global.SelectedPiece = piece.ToString();

                int index;
                Button destination = null;

                switch (piece)
                {
                    case ChessPiece.WPawn:
                        textBox3.Text = "W pawn";

                        index = buttons.IndexOf(btn);
                        destination = buttons[(index + 8) % buttons.Count];
                        Global.SelectedBtn = buttons[(index) % buttons.Count];

                        destination.Click += destination_clicked;

                        break;

                     // and other cases
                }

            }

        }

Upvotes: 0

Views: 66

Answers (2)

Coskun Ozogul
Coskun Ozogul

Reputation: 2469

You try to call a non-static method in a static method. It causes the problem.

And attention, if you assign with += an event to your button, it will multiply the click event.

It means that if you call 2 times buttons[i].Click += ChessBlock_clicked; when you will click on your button, it will execute the same method two times.

To release the click event, you can use buttons[i].Click -= ChessBlock_clicked; or before assigning it you can check it it is assigned.

Upvotes: 0

Scott Hannen
Scott Hannen

Reputation: 29212

This is a static method. ChessBlock_clicked is not. A static method can't call an instance (non-static) method.

There are a few ways to fix this. The simplest is that if this method needs to access a non-static method, it shouldn't be static.

Upvotes: 1

Related Questions