프로그래밍/C#
c# 동적으로 버튼 생성하고 그 버튼으로 메시지 박스 띄우기
뽀로로친구에디
2021. 11. 24. 10:47
c# 동적으로 버튼 생성하고 그 버튼으로 메시지 박스 띄우는 방법을 알아보겠습니다.
- YouTube
www.youtube.com
먼저 폼에 버튼한개 추가하고요. 더블 클릭해서 코딩하면 되요.

버튼을 몇개 만들건지
int count = 0;
이렇게 카운트 변수 하나 만들어 주시고요.
동적버튼 생성은 이렇게 하시면 됩니다.
private void button1_Click(object sender, EventArgs e)
{
Button btn_clone = new Button();
btn_clone.Click += new EventHandler(btn_clone_click);
this.Controls.Add(btn_clone);
btn_clone.Location = new Point(50 + (100 * count), 50);
btn_clone.Width = 60;
btn_clone.Height = 30;
btn_clone.FlatStyle = FlatStyle.Standard;
btn_clone.BackColor = Color.FromArgb(100, Color.Yellow);
btn_clone.Text = "button" + count.ToString();
count++;
}
private void btn_clone_click(object sender, EventArgs e)
{
Button btn = sender as Button;
MessageBox.Show(btn.Text);
}
