Modify attributes of a label with a dynamic name

Asked

Viewed 213 times

1

Example I did this because I am creating Controls and names dynamically, well. the problem is when I need to use or call these values.

If I want to change the value of the control your text or value ,something like that. I wonder if there’s a way to do it.

My code:

public int Playes = 0;

private void Criacao()
        {
             Playes += 1;

    string HP0 = "Lab" + Playes.ToString();  //Sempre que criar o controlle Adiciona 1 no int Playes, dai o nome fica algo como : "Lab1"

    Label L1 = new Label(); //Criação do label.

    L1.Name = HP0; // definir nome da label.
    L1.AutoSize = true; //Define o tamanho automaticamente.
        }

Simply put, I wonder if there’s any way to change the value of the controls, different example Lab1.text = "teste" or Lab2.text = "teste2";, because I’m changing the name in process I don’t know how to call it. (Sorry if this explained poorly, I’m still layman.)

  • Put the code that generates the controls dynamically because otherwise it is difficult to adapt the logic you used

2 answers

2

Try to use it this way:

public partial class Form1 : Form
{
    int contador = 1;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    public void AddNewLabel()
    {
        //Define posição top
        var top = contador * 25;

        //Novo label
        Label lbl = new Label();
        lbl.Top = top;
        lbl.Left = 10;
        lbl.Text = "Lab " + this.contador.ToString();
        lbl.Name = "Lab" + contador;
        //Adiciona no Form
        this.Controls.Add(lbl);

        //Incrementa Contador
        contador = contador + 1;
    }

    public void ChangeLabel()
    {
        foreach (Control control in this.Controls)
        {
            if (control is Label)
            {
                if (control.Name == "NOME DO LABEL CRIADO")
                {
                    //Remove o label
                    this.Controls.Remove(control);

                    //Adiciona o label novamente
                    control.Text = "TEXTO A SER ALTERADO";
                    this.Controls.Add(control);
                }
            }
        }
    }

    private void AdcionarLabel_Click(object sender, EventArgs e)
    {
        AddNewLabel();
    }

    private void TrocarTxtLabel_Click(object sender, EventArgs e)
    {
        ChangeLabel();
    }
}
  • Yes but my question is how I modify the label created after example Lab1.text = "hello"; do not want to create 5 label with the same name you understand? I want to modify one by one after.

  • Sorry, I didn’t understand. I corrected the code by adding the changelabel function responsible for changing the text at runtime.

0


After creating your controls:

// Exemplo de criação de 3 labels
for (int i = 1; i <= 3; i++)
{
    Label label = new Label()
    {
        Name = "Lbl" + i.ToString(),
        Text = $"Label " + i.ToString()
    };

    this.Controls.Add(label);
}

Just get them from the name and modify the properties you need (in this case only the Text):

for (int i = 1; i <= 3; i++)
{
    var label = this.Controls["Lbl" + i.ToString()] as Label;

    if (label != null)
        label.Text = string.Format(@"Label {0} (modified!)", i);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.