첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// FROM 텍스트 상자
        /// </summary>
        private TextBox fromTextBox;

        /// <summary>
        /// TO 텍스트 상자
        /// </summary>
        private TextBox toTextBox;

        /// <summary>
        /// 결과 레이블
        /// </summary>
        private Label resultLabel;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            Title         = "날짜 계산하기";
            SizeToContent = SizeToContent.WidthAndHeight;
            ResizeMode    = ResizeMode.CanMinimize;
            FontFamily    = new FontFamily("나눔고딕코딩");
            FontSize      = 16;

            Grid grid = new Grid();

            Content = grid;

            for(int i = 0; i < 3; i++)
            {
                RowDefinition rowDefinition = new RowDefinition();

                rowDefinition.Height = GridLength.Auto;

                grid.RowDefinitions.Add(rowDefinition);
            }

            for(int i = 0; i < 2; i++)
            {
                ColumnDefinition columnDefinition = new ColumnDefinition();

                columnDefinition.Width = GridLength.Auto;

                grid.ColumnDefinitions.Add(columnDefinition);
            }

            Label captionLabel = new Label();

            captionLabel.Content = "시작일 : ";

            grid.Children.Add(captionLabel);

            Grid.SetRow   (captionLabel, 0);
            Grid.SetColumn(captionLabel, 0);

            this.fromTextBox = new TextBox();

            this.fromTextBox.Text = new DateTime(1980, 1, 1).ToShortDateString();

            this.fromTextBox.TextChanged += textBox_TextChanged;

            grid.Children.Add(this.fromTextBox);

            Grid.SetRow   (this.fromTextBox, 0);
            Grid.SetColumn(this.fromTextBox, 1);

            captionLabel = new Label();

            captionLabel.Content = "종료일 : ";

            grid.Children.Add(captionLabel);

            Grid.SetRow   (captionLabel, 1);
            Grid.SetColumn(captionLabel, 0);

            this.toTextBox = new TextBox();

            this.toTextBox.TextChanged += textBox_TextChanged;

            grid.Children.Add(this.toTextBox);

            Grid.SetRow   (this.toTextBox, 1);
            Grid.SetColumn(this.toTextBox, 1);

            captionLabel = new Label();

            captionLabel.Content = "계산 결과 : ";

            grid.Children.Add(captionLabel);

            Grid.SetRow   (captionLabel, 2);
            Grid.SetColumn(captionLabel, 0);

            this.resultLabel = new Label();

            grid.Children.Add(this.resultLabel);

            Grid.SetRow   (this.resultLabel, 2);
            Grid.SetColumn(this.resultLabel, 1);

            Thickness thickness = new Thickness(5);

            grid.Margin = thickness;

            foreach(Control control in grid.Children)
            {
                control.Margin = thickness;
            }

            this.fromTextBox.Focus();

            this.toTextBox.Text = DateTime.Now.ToShortDateString();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application application = new Application();

            application.Run(new MainWindow());
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 텍스트 상자 텍스트 변경시 처리하기 - textBox_TextChanged(sender, e)

        /// <summary>
        /// 텍스트 상자 텍스트 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            DateTime fromDate, toDate;

            if(DateTime.TryParse(this.fromTextBox.Text, out fromDate) && DateTime.TryParse(this.toTextBox.Text, out toDate))
            {
                int tearCount  = toDate.Year  - fromDate.Year;
                int monthCount = toDate.Month - fromDate.Month;
                int dayCount   = toDate.Day   - fromDate.Day;

                if(dayCount < 0)
                {
                    dayCount += DateTime.DaysInMonth(toDate.Year, 1 + (toDate.Month + 10) % 12);

                    monthCount -= 1;
                }

                if(monthCount < 0)
                {
                    monthCount += 12;

                    tearCount -= 1;
                }

                this.resultLabel.Content = string.Format("{0}년 {1}개월 {2}일", tearCount, monthCount, dayCount);
            }
            else
            {
                this.resultLabel.Content = string.Empty;
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요