본문 바로가기
C#

label 에 대한 동적 이벤트

by improve 2024. 2. 22.
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 labeladdelbutton
{
public partial class Form1 : Form
{
    List<int> ints = new List<int>() ;
    int size = 3;
        
        public Form1()
    {
        InitializeComponent();
        //this.label2.Font = new System.Drawing.Font("휴먼엑스포", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));

        Console.WriteLine();


        setLabelList();
        addButton();



    }

        #region 버튼 추가하는 함수
    private void addButton()
    {
        Random ran = new Random();

        for (int i = 0; i < 5; i++)
        {
            string randomstr = ran.Next(100).ToString();
            // 추가 하는 버튼
            Button button = new Button();
            button.Text = randomstr;
            button.Font = new System.Drawing.Font("휴먼엑스포", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
            button.Location = new Point(80 + (i * 100), 230);
            button.AutoSize = true;
            button.Click += Button_Click;

            //button.Click += new System.EventHandler(Button_Click1);
            //button.Click += (sender, e) =>
            //{
            //    MessageBox.Show("람다도 되나"+sender+""+e);
            //};

            Controls.Add(button);


            // 삭제 하는 버튼 
            Button button1 = new Button();
            button1.Text = randomstr;
            button1.Font = new System.Drawing.Font("휴먼엑스포", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(129)));
            button1.Location = new Point(80 + (i * 100), 330);
            button1.AutoSize = true;
            button1.Click += (sender,e) =>
            {
                Button temp = sender as Button;
                int itemp = int.Parse(temp.Text);
                ints.Remove(itemp);

                setLabelList();
            };
            Controls.Add(button1);
        }
    }

        #endregion





    private void Button_Click2(object sender, EventArgs e)
    {

    }

    private void Button_Click1(object sender, EventArgs e)
    {
        MessageBox.Show("되나");
    }

    private void Button_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        //MessageBox.Show("test"+btn.Text);
        int temp = int.Parse(btn.Text);
        ints.Add(temp);
        setLabelList();
    }

    private void setLabelList()
    {
        string result = "";


        for (int i = 0; i < ints.Count; i++)
        {
            if (ints.Count != (i + 1))
            {
                result = result + (ints[i] + ",");
            }


            else
                result = result + (ints[i] + " ");
        }
        label2.Text = result;
    }


    //폼이 나오는순간 실행되는 함수 
    private void Form1_Load(object sender, EventArgs e)
    {
        //MessageBox.Show("Test");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("추가실행");
    }
}
}

 

 

디자인으로 버튼을 넣지 않고 동적으로 버튼을 생성하였다 .

list를 만들어서 추가버튼을 누를때 list에 추가 되고 

삭제 버튼을 누를때 list에서 삭제 된다.

'C#' 카테고리의 다른 글

C# crud(create, read(select), update, delete) 2  (0) 2024.02.26
C# crud(create, read(select), update, delete)  (0) 2024.02.23
C# 수업중 내용 최종 (입출력)  (0) 2024.02.21
c# 수업중 내용2  (0) 2024.02.20
파일 저장 및 불러오기  (0) 2024.02.20