본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - Enum 열거형 변수 사용법

728x90
반응형


 WPF & C# - Enum 열거형 변수 사용법




enumTest.zip




MainWindow.xaml.cs



1
2
3
4
5
6
7
8
9
10
11
12
13
<StackPanel>
    <Label Content="TEST 01" FontWeight="Bold" FontSize="18" Margin="5"></Label>
    <ComboBox x:Name="cbx" Height="30" SelectionChanged="cbx_SelectionChanged"/>
    <TextBox x:Name="txt" Text="TextBox"  FontSize="18" />
 
    <Label Content="TEST 02" FontWeight="Bold" FontSize="18" Margin="5"></Label>
    <ComboBox x:Name="cbx2" Height="30" SelectionChanged="cbx2_SelectionChanged"/>
    <TextBox x:Name="txt2" Text="TextBox" FontSize="18" />
 
    <Label Content="TEST 03" FontWeight="Bold" FontSize="18" Margin="5"></Label>
    <ComboBox x:Name="cbx3" Height="30" SelectionChanged="cbx3_SelectionChanged"/>
    <TextBox x:Name="txt3" Text="TextBox" FontSize="18" />
</StackPanel>
cs




MainWindow.xaml.cs



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
// 열거형
enum SMTP { Naver, Daum, Yahoo, Hotmail = 10 }
 
SMTP Server1;
SMTP Server2;
SMTP Server3;
void initUI()
{
    // 콤보박스에 enum 타입 입력
    {
        var values = Enum.GetValues(Server1.GetType());
        foreach (var value in values)
        {
            cbx.Items.Add(value);   // 입력값 : Naver, Daum, Yahoo, Hotmail
        }
    }
 
    // Enum 갯수 만큼 index 넘버 입력
    {
        var values = Enum.GetValues(Server2.GetType());
        for (int i = 0; i < values.Length; i++)
        {
            cbx2.Items.Add(i);   // 입력값 :  0, 1, 2, 3
        }
    }
 
    // Enum 갯수 만큼 index 넘버 입력
    // 콤보박스에 index 넘버 입력
    {
        var values = Enum.GetValues(Server3.GetType());
        for (int i = 0; i < values.Length; i++)
        {
            cbx3.Items.Add(((SMTP)i).ToString());   // 입력값 :  Naver, Daum, Yahoo, 3
        }
    }
}
 
// 콤보박스 
private void cbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // 콤보박스 선택 인덱스 = Enum 값 배열 인덱스 값 매칭
    Server1 = (SMTP)cbx.SelectedIndex;
    txt.Text = "Index : " + cbx.SelectedIndex.ToString() + " = " + Server1.ToString();
 
    // 콤보박스 선택 값 = Enum 값 배열 인덱스 매칭
    Server1 = (SMTP)cbx.SelectedValue;
    txt.Text += " || " + "Value : " + cbx.SelectedValue.ToString() + " = " + Server1.ToString();
 
    // 콤보박스 선택 아이템 = Enum 값 배열 인덱스 매칭
    Server1 = (SMTP)cbx.SelectedItem;
    txt.Text += " || " + " Item : " + cbx.SelectedValue.ToString() + " = " + Server1.ToString();
}
 
// 콥보박스 2
private void cbx2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // 콤보박스 선택 인덱스 = Enum 값 배열 인덱스 값 매칭
    Server2 = (SMTP)cbx2.SelectedIndex;
    txt2.Text = "Index : " + cbx2.SelectedIndex.ToString() + " = " + Server2.ToString();
 
    // 콤보박스 선택 값 = Enum 값 배열 인덱스 매칭
    Server2 = (SMTP)cbx2.SelectedValue;
    txt2.Text += " || " + "Value : " + cbx2.SelectedValue.ToString() + " = " + Server2.ToString();
 
    // 콤보박스 선택 아이템 = Enum 값 배열 인덱스 매칭
    Server2 = (SMTP)cbx2.SelectedItem;
    txt2.Text += " || " + " Item : " + cbx2.SelectedValue.ToString() + " = " + Server2.ToString();
}
 
// 콥보박스 3
private void cbx3_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // 콤보박스 선택 인덱스 = Enum 값 배열 인덱스 값 매칭
    Server3 = (SMTP)cbx3.SelectedIndex;
    txt3.Text = "Index : " + cbx3.SelectedIndex.ToString() + " = " + Server3.ToString();
}
cs








728x90
반응형