저는 일반적으로 C # 및 프로그래밍을 처음 사용합니다. Windows Form 애플리케이션과 Visual Studio를 사용하여 간단한 쇼핑 목록 앱 을 만들려고했습니다 . 이것이 내가 목록에 항목을 추가하는 방법입니다.
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
y = y + 25;
}
내가 가진 주요 문제는 다른 이벤트가 레이블 의 속성을 변경할 수 없다는 것 입니다 . 전의:
public Form1()
{
InitializeComponent();
}
int x = 50;
int y = 58;
private void addButton_Click(object sender, EventArgs e)
{
Label itemName = new Label();
itemName.Text = itemInput.Text;
itemInput.Text = "";
this.Controls.Add(itemName);
itemName.Location = new Point(x, y);
itemName.Width = 260;
CheckBox coupon = new CheckBox();
coupon.Location = new Point(x - 30, y);
this.Controls.Add(coupon);
Button deletButton = new Button();
deletButton.Text = "delete";
this.Controls.Add(deletButton);
deletButton.Location = new Point(x + 260, y);
deletButton.Width = 50;
y = y + 25;
}
private void deletButton_Click(object sender, EventArgs e)
{
itemName.Text = "";
}
그것은 말한다
현재 컨텍스트에 이름 itemName이 없습니다.
다른 방법에 있기 때문에 의미가 있습니다.
내 주요 질문은 해당 메서드 외부에서 itemName을 사용할 수 있도록 할 수 있습니까? 아니면 완전히 잘못되어 프로그램을 처음부터 다시 설계해야합니까?
지금하고있는 것처럼 컨트롤의 동적 추가를 고수하고 싶다고 가정 해 보겠습니다. 간단한 방법은 이름을 지정하고 해당 이름으로 찾는 것입니다.
// When you're creating it.
itemName.Name = "itemName";
// Finding it.
var itemName = (Label)this.Controls["itemName"];
// Another way to find it.
var itemName = (Label)this.Controls.Find("itemName", true);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다