저는 C# 프로그래밍의 완전한 멍청한 행동입니다. 이것은 내 문제입니다 : 버튼 1을 클릭하면 생성되는 패널이있는 양식이 있으며 패널에는 패널의 배경색을 변경하는 버튼 (btnColor1)도 있습니다. btnColor1에서 패널의 배경색을 참조하고 싶습니다만, "이름 'btnColor1'이 현재 컨텍스트에 존재하지 않습니다."라는 오류 메시지가 나타납니다. 이 문제를 어떻게 해결할 수 있습니까?C# 런타임에서 생성 된 버튼을 사용하여 패널의 속성 편집
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 TEST_APP_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Panel myPanel1 = new Panel();
myPanel1.Location = new System.Drawing.Point(100, 50);
myPanel1.Name = "Panel 1";
myPanel1.Size = new System.Drawing.Size(200, 100);
myPanel1.BackColor = Color.Red;
TextBox textBox1 = new TextBox();
textBox1.Location = new Point(10, 50);
textBox1.Text = "empty field";
textBox1.Size = new Size(150, 30);
Button btnColor1 = new Button();
btnColor1.Location = new Point(10, 10);
btnColor1.Text = "GOLD";
btnColor1.Size = new Size(100, 30);
btnColor1.Click += myButton1_Click;
myPanel1.Controls.Add(textBox1);
myPanel1.Controls.Add(btnColor1);
Controls.Add(myPanel1);
}
private void myButton1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
btnColor1.BackColor = Color.Gold;
}
}
}
당신이 새로운 버튼, 새로운 패널을 필요로 확신을 모든 버튼을 클릭 할 때마다 생성됩니까? 그렇지 않다면 클래스에 필드로'btnColor1'을 저장하면됩니다. –