-2

I don't want to repeat my code, so I want to make something that can change the text on multiple buttons. Is it any way you can specify what button is called and add that to the code for changing the text?

I know you can change text on a button with button1.Text = "X";

I have tried to make a string, so I only have to write the name of the button, so the calling code ends up being PickPlayer("button1");.

My full code is:

    private void button9_Click(object sender, EventArgs e)
    {
        pickPlayer("button9");
    }

    private void pickPlayer(string Button)
    {
        if (player % 2 == 0)
        {
           player += 1;
           Button.Text = "O";
        }
        else
        {
           player += 1;
           Button.Text = "X";
        }
    }

But it shows an error message as follows:

string does not contain a definition for Text.

What am I doing wrong?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Alexander
  • 23
  • 6
  • 2
    Instead of `string Button` receive `Button Button` and call your method like `pickPlayer((Button)sender);` – dcg Jun 23 '19 at 21:23
  • You are getting an error because you are trying to set the text property on a string, which you cannot do. I don't understand what you mean by "Is it any way you can write what the button is called an add that to the code for changing the text?". If your code had this ability, how would you determine what to make the button text? – Jake Steffen Jun 23 '19 at 21:33

1 Answers1

0

This will append Foo to the end of all of your buttons. Without knowing more information to what the conditions are for setting the button text, this is all I can give you.

 private void Form1_Load(object sender, EventArgs e)
    {
        var list =  GetAllButtonControls();
        foreach (Control control in list)
        {
            if (control.GetType() == typeof(Button))
            {
                //Set text
                control.Text += " Foo";
            }
        }


    }

    private List<Control> GetAllButtonControls()
    {
        List<Control> rlist = new List<Control>();
        foreach (Control control in this.Controls)
        {
            if (control.GetType() == typeof(Button))
            {
                rlist.Add(control);
            }

        }

        return rlist;
    }
Jake Steffen
  • 415
  • 2
  • 11
  • 1
    First, the OP isn't asking to modify _all_ buttons, but rather to generalize and use the same code on a _single_ button by name. Second, why would you go to all the trouble to filter the controls by type, adding only `Button` objects to the list, but then use `Control` as the list's element type, so that the caller then has to check the type again (and cast if necessary to get `Button`-specific members)? The method's name is `GetAllButtonControls()`, so why not just return `List – Peter Duniho Jun 23 '19 at 21:56
  • Why create a list at all just to throw it away microseconds later? If you did create a list at start up, better to save for the next time rather than fishing thru the controls collection over and over to recreate – Ňɏssa Pøngjǣrdenlarp Jun 23 '19 at 21:59