-1

I want to assign buttons from the string array, is it possible? I have tried:

     private void label1_TextChanged(object sender, EventArgs e)
    {
        
        searchedModels = LabelPaieskaText.Split(',');
        for (int i = 0; i < searchedModels.Count(); i++)
        {

            $"{btn_search} + {i + 1}".Text = searchedModels[i]; // this is the problem
            
        }

    }

1 Answers1

1

I dont know, how your Buttons are named. This is an example:

string name = "the_name_you_know";
Control ctn = this.Controls[name];
ctn.Text = "Example...";

You could also try to find it via find method: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.controlcollection.find?view=net-5.0

Try this:

private void label1_TextChanged(object sender, EventArgs e)
{
    searchedModels = LabelPaieskaText.Split(',');
    for (int i = 0; i < searchedModels.Count(); i++)
    {
        this.Controls[$"btn_search{i + 1}"].Text = searchedModels[i]; 
    }
}
Sebastian Siemens
  • 2,302
  • 1
  • 17
  • 24