Je suis en train de créer des étiquettes et les boutons de manière dynamique. Je veux changer le nom du libellé de façon dynamique dans des caisses clic sur le bouton. Quand je suis en train d'écrire le button_click méthode, je ne peux pas accéder à l'étiquette de l'objet directement. Comment puis-je le faire?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DemoPanel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int lblYVal = 10;
int btnYVal = 50;
for(int i = 1; i< 5; i++)
{
Label lbl = new Label();
lbl.Text = "test";
lbl.Name = "test"+i.ToString();
lbl.Location = new System.Drawing.Point(10, lblYVal);
lbl.Visible = true;
Button btn = new Button();
btn.Text = "Click";
btn.Name = "textBtn" + i.ToString();
btn.Location = new System.Drawing.Point(10,btnYVal);
btn.Visible = true;
btn.Click += new EventHandler(this.btn_click);
this.Controls.Add(lbl);
this.Controls.Add(btn);
lblYVal += 70;
btnYVal += 70;
}
}
void btn_click(object sender, EventArgs e)
{
//How can i change label text from here.
//lbl.text //Does Not exist Error.
Label lbl = new Label();
lbl.Text = "New text"; //Not changing Label text
}
}
}