본문 바로가기

   
Programming/Winform

동적 버튼 생성 동적 버튼 액션 생성

반응형

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Test

{

       public partial class Teachar01 : Form

       {

             private int x;

             private int y;

             private int count;

 

             public Teachar01()

             {

                    InitializeComponent();

                    x = 10;

                    y = 10;

                    count = 1;

             }

 

             private void Teachar01_Load(object sender, EventArgs e)

             {

                    for (int i = 0; i < 5; i++)

                    {

                           //폼로드 : 프로그램이 시작된다.

                           // -> 동적으로 버튼을 4개 생성해서 추가해주세요

                           Button btn = new Button();

                           btn.Text = string.Format("버튼{0}", count);

                           btn.Size = new Size(80, 60);

                           btn.Location = new Point(x, y);

 

                           //델리게이트를 통해 버튼 이벤트 생성

                           btn.Click += new EventHandler(btn_Click);

 

                           //화면에 추가

                           //조건 : 컨테이너 컨트롤의 자식으로 추가

                           //현재 폼 : 메인폼, 그룹박스1, 그룹박스2

                           //컨테이너 컨트롤은 Controls 속성의 컬렉션으로 자식들 관리 -> Add() 자식을 추가 -> Reove(), RemoveAt() 자식을 삭제

                           this.groupBox2.Controls.Add(btn);

 

                           count += 1;

                           x += 90;

                    }

             }

 

             void btn_Click(object sender, EventArgs e)

             {

                    this.groupBox2.Controls.Remove((Button)sender);

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    //클릭이 된 버튼

                    //button1 = sender(동일 인물)

 

                    //클릭이 된 버튼을 삭제

                    //클릭이된 버튼이 누구의 자식인지를 확인하고 -> 그부모의 Controls 컬렉션에서 클랙된 버튼을 Remove();

                    //this.groupBox1.Controls.Remove(button1);                 

                    //Button btn = (Button)sender;

                    this.groupBox1.Controls.Remove((Button)sender);

             }

       }

}

 

 


 

 using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

 

namespace Test

{

       public partial class Teachar02 : Form

       {

             private int y;

             private StreamReader reader;

             private string name;

 

             public Teachar02()

             {

                    InitializeComponent();

                    y = 20;

                    //list.txt 읽을 수 있는 StreamReader 생성

                    //1.Fileinfo -> 2. OpenText() -> StreamReader

                    reader = new StreamReader("list.txt");

             }

 

             private void Teachar02_Load(object sender, EventArgs e)

             {

                    for (int i = 0; i < 7; i++)

                    {

                           //명단에서 한명씩 가져오기

                           string name = reader.ReadLine();

 

                           Button btn = new Button();

                           btn.Text = name;

                           btn.Size = new Size(groupBox1.Width - 10, 35);

                           btn.Location = new Point(5, y);

 

                           groupBox1.Controls.Add(btn);

                           y += 40;

                    }

                    reader.Close();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    RemoveName();

             }

 

             private void RemoveName()

             {

                    //1. 텍스트박스의 값을 가져오기(삭제 대상)

                    //2. 이름 명단을 가져오기(그룹박스의 자식인 컬렉션)

                    //3. 명단에서 사람찾기

                    //4. 찾으면..삭제(그룹박스에서 Remove())

                    //5. 못찾으면 .. 패스

                    name = this.textBox1.Text;

 

                    //3

                    for (int i = 0; i < this.groupBox1.Controls.Count; i++)

                    {

                           Button btn = (Button)this.groupBox1.Controls[i];

 

                           if (btn.Text == name)

                           {

                                 //너 사라져

                                 groupBox1.Controls.Remove(btn);

 

                                 //사용자 편의성 향상

                                 textBox1.Text = "";

                                 textBox1.Focus();

                                 break;

                           }

                    }

             }

 

             private void textBox1_KeyDown(object sender, KeyEventArgs e)

             {

                    //엔터를 치면 검색버튼을 누른것과

                    if (e.KeyCode == Keys.Enter)

                    {

                           RemoveName();

                    }

             }

       }

}

 

 


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace User01

{

       public partial class Form1 : Form

       {

 

             private int x;

             private int y;

             private bool yorn;

 

             public Form1()

             {

                    InitializeComponent();

                    x = 10;

                    y = 10;

                    yorn = true;

             }

 

             private void Form1_Load(object sender, EventArgs e)

             {

                    Add();

             }

 

             private void Add()

             {

                    y = 10;

                    for (int i = 0; i < 7; i++)

                    {

                           TextBox txt = new TextBox();

                           txt.Location = new Point(x, y);

                           txt.Size = new Size(200, 200);

                           this.groupBox1.Controls.Add(txt);

 

                           y += 30;

                           txt.Enter += new EventHandler(txt_Enter);

                           txt.Leave += new EventHandler(txt_Leave);

                    }

             }

 

             void txt_Leave(object sender, EventArgs e)

             {

                    TextBox t = (TextBox)sender;

                    t.BackColor = Color.White;

             }

 

             void txt_Enter(object sender, EventArgs e)

             {

                    TextBox t = (TextBox)sender;

                    t.BackColor = Color.Yellow;

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    if (yorn)

                    {

                           this.Size = new Size(this.Width, this.Height + 80);

                          

                          

                           yorn = false;

                    }

                    else

                    {

                           this.Size = new Size(this.Width, this.Height-80);

                           yorn = true;

                    }

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    this.groupBox1.Controls.Clear();

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    Add();

             }

       }

}

 

 

반응형