DBILITY

c# Enumeration foreach 본문

C#

c# Enumeration foreach

DBILITY 2019. 2. 15. 10:44
반응형

기록해 두자.

어차피 기억하지 못할테니까..ㅎㅎ

리스트박스에 아이템을 추가하는 예제다.

 

 

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 ex001
{
    public partial class ExamEnum : Form
    {
        public ExamEnum()
        {
            InitializeComponent();
        }

        string strName = ":D";

        private enum enumDay
        {
            Monday = 1,
            Tuesday = 2,
            Wednesday = 3,
            Thursday = 4,
            Friday = 5,
            Staturday = 6,
            Sunday = 0
        }

        private enum enumTime
        {
            Morning,
            Afternoon,
            Evening
        }

        private void ExamEnum_Load(object sender, EventArgs e)
        {

            tboxName.Text = strName;
               
            foreach(enumDay val in Enum.GetValues(typeof(enumDay)))
            {
                lboxDay.Items.Add(val);
            }

            foreach (enumTime val in Enum.GetValues(typeof(enumTime)))
            {
                lboxTime.Items.Add(val);
            }

        }

        private void btnResult_Click(object sender, EventArgs e)
        {
            string strResult = tboxName.Text + " " + lboxDay.SelectedItem.ToString() + " " + lboxTime.SelectedItem.ToString();
            tboxResult.Text = strResult;
        }

        private void btnStringFormat_Click(object sender, EventArgs e)
        {
            string strResult = String.Format("{0} , {1} , {2}", tboxName.Text, lboxDay.SelectedItem, lboxTime.SelectedItem);
            tboxResult.Text = strResult;
        }
    }
}

 

반응형
Comments