티스토리 뷰

======================================================

안녕하세요 Doridori 입니다.


이번시간은 지난 강의와 이어지는 내용으로 Layout을 만들었으니 Layout간 Event의 이동에 대해서 알아보도록 하겠습니다. 


내용만 보면 지난 Delegate 부분과 비슷하다고 볼 수 있지만 실제로 사용해봐야 내것이 되는것이 아니겠습니까!! 


그외 내용으로 UI에서 Control 검색하기, delegate, Action, EventHandler 외 FlowlayoutPanel 등의 내용이 있으므로 한번 보시면 도움이 될것 같습니다. ^^;;


짧을줄알고 빨리 준비 했는데 이것저것 넣다보니 강의가 또 길어졌습니다. ㅎㅎ;;


화이팅 입니다.~! 

======================================================


34. Layout 간 Event 제어 (FlowLayoutPanel)

 

 

Source UI) (지난강의 전체 Source 부터 시작 하시면 됩니다.)

Soruce 전체) Study_34_Layout Event Control.zip

교재) 34. Layout 간 Event 제어 (FlowLayoutPanel).pdf



 

 

 

지난 시간 Layout에 대해서 진행 했을 했는데 기왕 만들 Layout에 Event를 넣어서 Event들이 진행 되는 부분을 확인 하면 좋을것 같아서 만들게 되었습니다. 


개인적으로는 프로그램을 개발 할 때 가장 중요하게 생각하는 부분이 Event 시점이라고 생각하는데 Event를 넘겨주는 부분들과 처리하는 부분들을 명확하게 해야 코드 분리가 쉬워지기 때문입니다. 


Event의 흐름 및 Event를 등록하는 방법들에 대해서 고민해 보시면 도움이 많이 될것 입니다. ^^;;



UI) 


지난강의 UI에서 몇가지만 고쳐서 만들었습니다. (라고 하기에는 시간이 너무 걸리네요 ㅎㅎ;;)


그냥 놔두기에는 조금 심심해서 아래 부분에 Event의 진행 Log를 볼 수 있는 부분을 추가 하였습니다. 


강의)


크게 별다른 내용이 없기는 한테 Event의 흐름을 설명하는게 생각보다 어려운것 같습니다. 


프로그램 소스 자체는 크게 어렵지 않지만 중단점을 걸어 놓고 넘어가는 부분들을 하나씩 확인해보시면 도움이 많이 될것 같습니다. 






Source)


UserControl을 두개를 만들어 놓았기 때문에 확인하면서 보시면 좋을것 같습니다. 



Main)


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
85
86
87
88
89
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 _33.Layout
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        /// <summary>
        /// Form Load Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            // ucCMenu Event 등록
            ucCMenu.eColorAction += UcCMenu_eColorAction;
 
            // ucPanel Event 등록
            ucPanelTop.eLabelDoubleClickHandler += UcPanel_eLabelDoubleClickHandler;
            ucPanelCenter1.eLabelDoubleClickHandler += UcPanel_eLabelDoubleClickHandler;
            ucPanelCenter2.eLabelDoubleClickHandler += UcPanel_eLabelDoubleClickHandler;
            ucPanelRight.eLabelDoubleClickHandler += UcPanel_eLabelDoubleClickHandler;
        }
 
 
        /// <summary>
        /// ucCMenu Event Function
        /// </summary>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        private void UcCMenu_eColorAction(Button arg1, Color arg2)
        {
            //Button obtn = arg1 as Button;
 
            string strPanelNeme = string.Empty;
 
            switch (arg1.Name)
            {
                case "btn1":
                    ucPanelTop.BackColor = arg2;
                    strPanelNeme = "Panel Top";
                    break;
                case "btn2":
                    ucPanelCenter1.BackColor = arg2;
                    strPanelNeme = "Panel Center1";
                    break;
                case "btn3":
                    ucPanelCenter2.BackColor = arg2;
                    strPanelNeme = "Panel Center2";
                    break;
                case "btn4":
                    ucPanelRight.BackColor = arg2;
                    strPanelNeme = "Panel Right";
                    break;
                default:
                    break;
            }
 
            string strResult = string.Format("선택 : {0}, {1}의 색상을 {2}로 변경", arg1.Name, strPanelNeme, arg2.ToString());
            lboxLog.Items.Add(strResult);                
        }
 
        /// <summary>
        /// ucPanel Event Function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UcPanel_eLabelDoubleClickHandler(object sender, EventArgs e)
        {
            string strResult = ucCMenu.fButtonColorChange((ucPanel)sender);
 
            lboxLog.Items.Add(strResult);
        }
 
    }
}
 
cs




ucColorMenu)


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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace _33.Layout
{
    public partial class ucColorMenu : UserControl
    {
        // 1) Delegate Event 선언
        public delegate void delColorSender(object oSender, Color oColor);
        public event delColorSender eColorSender;
 
        // 2) 기본 EventHandler
        public event EventHandler oColorEventHandler;
 
        // 3) 제네릭 형태의 delegate 사용
        public event Action<Button, Color> eColorAction;
 
 
        public ucColorMenu()
        {
            InitializeComponent();
        }
 
 
        /// <summary>
        /// Control Load Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ucColorMenu_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 5; i++)
            {
                Button obtn = new Button();
 
                obtn.Name = "btn" + i;
                obtn.Text = string.Format("P{0} 색상 변경", i);
                obtn.BackColor = Color.Gray;
                obtn.Margin = new Padding(102000);
                obtn.Size = new Size(10030);
                obtn.Click += Obtn_Click;
 
                flpMenu.Controls.Add(obtn);
            }
        }
        
        /// <summary>
        /// obtn Event Function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Obtn_Click(object sender, EventArgs e)
        {
            //eColorSender(sender, pColor.BackColor);  // 1) Delegate Event에 대한 사용
            //oColorEventHandler(sender, e);   // 2) 기본 EventHandler에 대한 사용
            eColorAction((Button)sender, pColor.BackColor);    // 3) 제네릭 형태의 delegate 사용
        }
 
 
        /// <summary>
        /// PColor에 대한 Click Event Function
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pColor_Click(object sender, EventArgs e)
        {
            DialogResult dRet = cDialogColor.ShowDialog();  // ColorDialog를 불러서 선택한 뒤 결과 값까지 받아 옴
 
            if (dRet == DialogResult.OK)   //결과 값이 정상일 때 선택 된 Color를 Panel에 뿌려줌
            {
                pColor.BackColor = cDialogColor.Color;
            }
        }
 
 
        /// <summary>
        /// 외부에서 Color Button의 색상을 변경하기 위한 Public Function
        /// </summary>
        /// <param name="oPanel"></param>
        /// <returns></returns>
        public string fButtonColorChange(ucPanel oPanel)
        {
            string strResult = string.Empty;
            string strbtnName = string.Empty;
 
            switch (oPanel.Name)
            {
                case "ucPanelTop":
                    strbtnName = "btn1";
                    break;
                case "ucPanelCenter1":
                    strbtnName = "btn2";
                    break;
                case "ucPanelCenter2":
                    strbtnName = "btn3";
                    break;
                case "ucPanelRight":
                    strbtnName = "btn4";
                    break;
                default:
                    break;
            }
 
            strResult = fBtnSearch(strbtnName, oPanel.BackColor, oPanel.Name);
            return strResult;
        }
 
 
        /// <summary>
        /// FlowLayoutPanel에서 원하는 Control을 찾아오기 위한 함수
        /// </summary>
        /// <param name="strButtonName"></param>
        /// <param name="oColor"></param>
        /// <param name="strPanelName"></param>
        /// <returns></returns>
        private string fBtnSearch(string strButtonName, Color oColor, string strPanelName)
        {
            string strResult = string.Empty;
 
            foreach (var oitem in flpMenu.Controls)
            {
                if (oitem is Button)
                {
                    Button obtn = oitem as Button;
 
                    if (obtn.Name.Equals(strButtonName))
                    {
                        obtn.BackColor = oColor;
                        strResult = string.Format("{0} Panel DoubleClick, {1}의 색상을 {2}로 변경", strPanelName, strButtonName, oColor.ToString());
                        return strResult;
                    }
                }
            }
 
            return null;
        }
        
    }
}
 
cs




ucPanel)


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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace _33.Layout
{
    public partial class ucPanel : UserControl
    {
        public event EventHandler eLabelDoubleClickHandler;
 
        public ucPanel()
        {
            InitializeComponent();
        }
        
 
        /// <summary>
        /// Panel Size가 변경될 때 화면에 Size를 보여주기 위함
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ucPanel_SizeChanged(object sender, EventArgs e)
        {
            lblPanel.Text = string.Format("({0},{1})", lblPanel.Width, lblPanel.Height);
        }
 
 
        /// <summary>
        /// lblPanel을 DoubleClick 했을 경우 Main으로 Event를 전달
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lblPanel_DoubleClick(object sender, EventArgs e)
        {
            eLabelDoubleClickHandler(this, e);
        }
    }
}
 
cs



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함