728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 라운드 버튼 사용하기 (0) | 2018.03.18 |
---|---|
[C#/WPF] 스타일러스 그림자 처리하기 (0) | 2018.03.18 |
[C#/WPF] Jeu de Tacquin (숫자 퍼즐 게임) (0) | 2018.03.18 |
[C#/WPF] Button 클래스 : 버튼 장식하기 (0) | 2018.03.18 |
[C#/WPF] 색상 선택하기 (0) | 2018.03.18 |
[C#/WPF] 디렉토리 탐색하기 (0) | 2018.03.18 |
[C#/WPF] RichTextBox 클래스 사용하기 (0) | 2018.03.18 |
[C#/WPF] ToggleButton 클래스 사용하기 (0) | 2018.03.10 |
[C#/WPF] CommandBinding 클래스 사용하기 (0) | 2018.03.10 |
[C#/WPF] FrameworkElement 클래스 : 커스텀 엘리먼트 사용하기 (0) | 2018.03.10 |
댓글을 달아 주세요