Click event for button matrix

0

I am creating an array of buttons using this code.

        private void Prueba_Load(object sender, EventArgs e)
    {
        int num = Negocios_Categorias_Menu.NroCategorias();
        DataTable ListaCat;
        ListaCat = Negocios_Categorias_Menu.Mostrar();
        int vertical1 = num / 3;
        int vertical2 = num % 3;
        if (vertical2!=0)
        {
            vertical1 = vertical1 + 1;
        }
        Button[,] boton = new Button[15, 3];
        int z = 0;
        for (int i = 0; i < vertical1; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (z < num)
                {
                    boton[i, j] = new Button();
                    boton[i, j].Width = 100;
                    boton[i, j].Height = 100;
                    boton[i, j].Text = ListaCat.Rows[z]["Categoria_Menu"].ToString(); ;
                    boton[i, j].Top = i * 100;
                    boton[i, j].Left = j * 100;
                    groupBox1.Controls.Add(boton[i,j]);
                    z++;
                }
            }
        }
    }

My problem is when creating the event for a click on the buttons, I have tried several solutions proposed in these forums but I have not gotten it to work.

    
asked by Andres Cabrera 14.01.2017 в 05:46
source

1 answer

2

Load it manually by loading the buttons as follows ...

    ...
    Button[,] boton = new Button[15, 3];
    int z = 0;
    for (int i = 0; i < vertical1; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if (z < num)
            {
                boton[i, j] = new Button();
                boton[i, j].Width = 100;
                boton[i, j].Height = 100;
                boton[i, j].Text = ListaCat.Rows[z]["Categoria_Menu"].ToString(); ;
                boton[i, j].Top = i * 100;
                boton[i, j].Left = j * 100;
                boton[i, j].Click += new EventHandler(this.GreetingBtn_Click);
                groupBox1.Controls.Add(boton[i,j]);
                z++;
            }
        }
    }
    ...

You immediately create the method that makes the event ....

     void GreetingBtn_Click(Object sender, EventArgs e)
     {
          // Cuando se le da click al boton,
          // solo le cambiaremos el texto y lo ponemos enabled.

          Button clickedButton = (Button)sender;
          clickedButton.Text = "Botón Click";
          clickedButton.Enabled = false;
     }

Try it is that I do not know how to work in arrangement, I've tried it just for a button ...

    
answered by 14.01.2017 / 06:44
source