기계는 거짓말하지 않는다

C# Windows Forms 이벤트 등록 본문

C#

C# Windows Forms 이벤트 등록

KillinTime 2021. 7. 16. 23:11

C# Windows Forms에 버튼, 텍스트 박스 등 여러 컨트롤에 이벤트를 등록할 수 있다.

하나 또는 그 이상의 이벤트 등록이 가능하다.

 

폼 버튼

Event 정의

private void button1_Click(object sender, EventArgs e)
{
	MessageBox.Show("Event Method Name: button1_Click");
}

private void Message1(object sender, EventArgs e)
{
	MessageBox.Show("Event Method Name: Message1");
}

private void Message2(object sender, EventArgs e)
{
	MessageBox.Show("Event Method Name: Message2");
}

Forrm의 Designer.cs 파일에서의 등록

// 
// button1
// 
this.button1.Location = new System.Drawing.Point(466, 58);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(94, 40);
this.button1.TabIndex = 5;
this.button1.Text = "버튼";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.Click += new System.EventHandler(this.Message1);
this.button1.Click += new System.EventHandler(this.Message2);

또는 Form 로드 시 등록

private void Form1_Load(object sender, EventArgs e)
{
    button1.Click += button1_Click;
    button1.Click += Message1;
    button1.Click += Message2;
}

 

버튼 클릭 시 왼쪽부터 차례대로 동작

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

C# try, catch, finally Exception 처리  (0) 2022.05.06
C# Thread, Thread 매개변수  (0) 2022.04.16
C# is, as, typeof 키워드  (0) 2021.07.16
C# partial class  (0) 2021.07.15
C# Windows Forms MenuStrip Check  (0) 2021.07.13
Comments