본문 바로가기

   
Programming/Winform

버튼 클릭시 색 넣기를 한번에 처리하기, 메소드로 묵어서 처리를 단계별로 보여줌

반응형

모든 컨트롤은 Tag가 존재한다 모든지 다넣을수 있다
Tag : 빈방, 넣고 싶은걸 마음대로 넣는다.

 

 

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 WindowsForms01

{

       public partial class Button02 : Form

       {

             public Button02()

             {

                    InitializeComponent();

             }

 

             private void button1_Click(object sender, EventArgs e)

             {

                    this.BackColor = Color.Red;

                    this.Text = "Red";

             }

 

             private void button2_Click(object sender, EventArgs e)

             {

                    this.BackColor = Color.Yellow;

                    this.Text = "Yellow";

             }

 

             private void button3_Click(object sender, EventArgs e)

             {

                    this.Text = "blue";

                    this.BackColor = Color.Blue;

             }

 

             private void button6_Click(object sender, EventArgs e)

             {

                    this.SetColor(Color.Red);

             }

 

             private void button5_Click(object sender, EventArgs e)

             {

                    this.SetColor(Color.Yellow);

             }

 

             private void button4_Click(object sender, EventArgs e)

             {

                    this.SetColor(Color.Blue);

             }

 

             private void SetColor(Color color)

             {

                    this.BackColor = color;

                    this.Text = color.ToString();

             }

 

             private void Btn(object sender, EventArgs e)

             {

                    //빨강, 노랑, 파랑 버튼이 모두 호출하는 이벤트 메서드

                    //문제점 : 누가 날 불렀는지???

                    //MessageBox.Show("하하");

 

                    //object sender : 이벤트를 발생시킨 객체(컨트롤)

                    Button btn = (Button)sender;

 

                    //MessageBox.Show(btn.Text);

 

                    if (btn.Text == "빨강")

                    {

                           this.BackColor = Color.Red;

                    }

                    else if (btn.Text == "노랑")

                    {

                           this.BackColor = Color.Yellow;

                    }

 

                    else if (btn.Text == "파랑")

                           this.BackColor = Color.Blue;

             }

 

             private void Button_SetColor(object sender, EventArgs e)

             {

                    //네번째

                    Button btn = (Button)sender;

                    this.BackColor = btn.BackColor;

             }

 

             private void Button_BackColor(object sender, EventArgs e)

             {

                    //다섯번째

                    //object sender로 어떤 버튼이 클릭 됐는지 알아온다.

                    Button btn = (Button)sender;

                    //다운 캐스팅

                    Color c = (Color)btn.Tag;

                    this.BackColor = c;

             }

 

             private void Button02_Load(object sender, EventArgs e)

             {

                    //컨트롤 초기 작업

                    this.button15.Tag = Color.Red;

                    this.button14.Tag = Color.Yellow;

                    this.button13.Tag = Color.Blue;

             }

 

       }

}

 

 

반응형