[c#] Event를 이용해 Label font size와 color change 구현




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        //1. 라벨 파라메터를 받을수 있는 대리자 선언
        public delegate void myDelegate(Label label);
 
        //2. 이벤트 선언
        public event myDelegate myEvent;
 
        //3. 이벤트를 발생시킬수 있는 메서드 만들기
        public void PushEvent(Label label)
        {
            //4. 이벤트가 등록된 경우에, 이벤트를 발생시켜라..
            if (myEvent != null)
            {
                myEvent(label);
            }
        }
 
 
        private void button1_Click(object sender, EventArgs e)
        {
            //5. 이벤트 발생!!
            PushEvent(label1);
        }
 
        public Form1()
        {
            InitializeComponent();
 
            //6. 첫번째 이벤트를 등록시킨다
            myEvent += new myDelegate(ColorChange);
 
            //7. 두번째 이벤트를 등록시킨다
            myEvent += new myDelegate(FontSizeChange);
        }
 
 
        /// <summary>
        /// 라벨 색상을 변경한다
        /// </summary>
        /// <param name="label">라벨</param>
        public void ColorChange(Label label)
        {
            Random r = new Random();
 
            label.ForeColor = Color.FromArgb(r.Next(0255), r.Next(0255), r.Next(0255));
 
            Console.WriteLine("DEBUG1:{0}", label.ForeColor);
 
            return;
        }
 
 
        /// <summary>
        /// 라벨 폰트 사이즈를 변경한다
        /// </summary>
        /// <param name="label">라벨</param>
        public void FontSizeChange(Label label)
        {
            Random r = new Random();
 
            label.Font = new Font(label.Font.FontFamily, r.Next(1,50));
 
            Console.WriteLine("DEBUG2:{0}", label.Font.Size);
 
            return;
        }
 
    }
 
}
 
cs

댓글

이 블로그의 인기 게시물

[c#] DataTable을 dataGridView에 바인딩

[React] index.html 수정하기

[React] 프로젝트 생성