0

[C#] Hello, I have a little problem with my program. I'm creating textboxes and a button each time a button is pressed. I want to create a method for each button through loop, and later on, be able to call that method so that when the button gets clicked, the textboxes and the button itself are deleted. How do I create these methods? How do I call each one of these respectively to button names(e.g button01, button02... button35) in a loop? I made one to test it but it does not work. Could anyone point me in a right direction? What to do?

TextBox fieldA = new TextBox();
            fieldA.Name = "fieldA" + i;
            fieldA.Text = "fieldA"+i;
            fieldA.Location = new Point(x[0], y[i]);
           Controls.Add(fieldA);
            textBoxListA[i] = fieldA;


            Button buttonA = new Button();
            buttonA.Name = "button" + i; // its name is  button0 
            buttonA.Text = "button" + i;
            buttonA.Location= new Point(x[3], y[i]);
            Controls.Add(buttonA); 

private void button0_Click(object sender, EventArgs e)] // im trying to call it but it wont work 
    {
        //code to delete textboxes and this button
    }
Matt0G
  • 1
  • 2
  • `buttonA.Click += new System.EventHandler(button0_Click);` Remember to `-=` before deleting the button. – Crowcoder Nov 09 '17 at 13:03
  • The thing is, I need it for all the buttons, what I want to achieve is buttonA.Click += new System.EventHandler("button"+i_Click); So I expect methods: button0_Click button1_Click and so on.I just dont know how to type it properly, i really hope that you understand what I mean – Matt0G Nov 09 '17 at 13:23
  • Typically you don't really need to do that. You can share one handler and manage button-specific logic within it's code. If you really need to then you can use a delegate as shown in the answer to the duplicate question. – Crowcoder Nov 09 '17 at 13:28

0 Answers0