④ C# 프로젝트 -9 자동으로 생성한 버튼을 누르면 메시지박스 보여주기 == Show the message box by pressing the automatically created button
C# 프로젝트 -8에서 텍스트 박스에 버튼 이름을 입력하고 버튼 생성하기를 누르면 버튼을 flowLayoutPannel에 추가 시켜 주었습니다.
이번에는 그 추가된 버튼에 event를 주어서 그 버튼이 클릭되면 메시지 박스가 나오게 만들었습니다.
buttonGroup.Last().Click += new EventHandler(Btn_Click);
이벤트를 주는 코드입니다.
Btn_Click에 대한 것을 하라는 내용입니다.
Btn_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;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public List<Button> buttonGroup = new List<Button>();
Boolean hwak = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (txtbox.Text.Equals("") || txtbox.Text.Equals("버튼의 이름을 입력하세요"))
{
MessageBox.Show("버튼 이름을 입력하세요");
}
else
{
for (int i = 0; i < buttonGroup.Count; i++)
{
if (buttonGroup[i].Name.Equals(txtbox.Text+"Btn"))
{
hwak = true;
}
}
if (hwak == true)
{
MessageBox.Show("해당 버튼의 이름이 있습니다.");
hwak = false;
}
else
{
buttonGroup.Add(new Button());
buttonGroup.Last().Name = txtbox.Text + "Btn";
buttonGroup.Last().Text = txtbox.Text;
buttonGroup.Last().Font = new System.Drawing.Font("맑은 고딕", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(50)));
this.flowLayoutPanel1.Controls.Add(buttonGroup.Last());
buttonGroup.Last().Click += new EventHandler(Btn_Click);
buttonGroup.Last().Visible = true;
}
}
}
public void Btn_Click(object sender, EventArgs e)
{
MessageBox.Show("버튼이 눌렸습니다.");
}
private void txtbox_MouseClick(object sender, MouseEventArgs e)
{
txtbox.Text = "";
}
}
}