을 만들려고 일부 레이블과 버튼합니다. 변경하고자 하는 상표의 이름에서 동적으로 crated 버튼을 클릭합니다. 내가 쓰 button_click 방법에 액세스 할 수 없습니다 라벨 객체를 직접 있습니다. 렇게 하려면 어떻게 해야 합니까?
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
}
}
}