티스토리 뷰

c# 동적으로 버튼 생성하고 그 버튼으로 메시지 박스 띄우는 방법을 알아보겠습니다. 

https://youtu.be/qPjoyDaQdjk

 

- 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);
        }

 

댓글
최근에 달린 댓글
글 보관함
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday
    뽀로로친구에디
    최근에 올라온 글