이벤트의 object sender, EventArgs e 매개 변수에 대해서
private void button1_Click(object sender, EventArgs e)
{
}
e 는 이벤트에 대한 정보가 담겨 있는 객체이다.
sender 는 이벤트가 일어나는 추체로 지금은 button 이다.
private void button1_Click(object sender, EventArgs e)
{
Button aButton;
aButton = sender as Button;
aButton.Text = "지옥으로 키티 ";
}
와 같이 직접적으로 버튼을 다룰 수 있다.
CheckBox
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 _210608_006
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int width = 100;
int height = 23;
int margin = 3;
CheckBox aCheckBox = new CheckBox()
{
Text = "감자",
Width = width,
Height = height,
Location = new Point(10, height * 0 + margin)
};
CheckBox bCheckBox = new CheckBox()
{
Text = "고구마",
Width = width,
Height = height,
Location = new Point(10, height * 1 + margin)
};
CheckBox cCheckBox = new CheckBox()
{
Text = "토마토",
Width = width,
Height = height,
Location = new Point(10, height * 2 + margin)
};
Button aButton = new Button()
{
Text = "클릭",
Width = width,
Height = height,
Location = new Point(10, height * 3 + margin)
};
aButton.Click += ButtonCluck;
Controls.Add(aCheckBox);
Controls.Add(bCheckBox);
Controls.Add(cCheckBox);
Controls.Add(aButton);
}
void ButtonCluck(object o, EventArgs e)
{
List<string> list = new List<string>();
foreach (var item in Controls)
{
// Controls 안에 Add 한 요서 모두 존재
if (item is CheckBox)
{
CheckBox checkBox = (CheckBox)item;
if (checkBox.Checked)
{
list.Add(checkBox.Text);
}
}
if (item is Button)
{
Button button = (Button)item;
MessageBox.Show(button.Text);
}
}
MessageBox.Show(string.Join(",", list)); //string.Join(",", list) : string 연결 시키는 함수
}
/* 배열형식으로도 접근가능
void ButtonCluck(object o, EventArgs e)
{
List<string> list = new List<string>();
//MessageBox.Show(string.Format($"{Controls.Count}")); //Controls.Count} 의 갯수를 가진다.
for (int i = 0; i < Controls.Count; i++)
{
MessageBox.Show(Controls[i].Text);
}
}
*/
}
}
Controls 위 예제에서 알수 있다 싶이 Controls Add 한 모든 부품을 다가지고 있다.
따라서 Controls 를 통해서 모든 member 에게 접근가능하다.
'공부,일 > C#' 카테고리의 다른 글
예외처리 (0) | 2021.06.10 |
---|---|
enum 열거형 다중지정 방식 (0) | 2021.06.10 |
소코반게임 만들기 (0) | 2021.06.08 |
210607 (0) | 2021.06.07 |
window forms (0) | 2021.06.04 |
댓글