大復活C#学習_Formで暦変換のプロジェクトを作る

f:id:yasui_swift:20210225051004j:plain

日の出まぎわの丘

.NET Framework フォームを用いて干支を調べるプロジェクトを作成してみた。CUIで作ったものを元にして作成してみたが、まだすっきりできない。

例外処理をボタン下にwhile(true)メソッドで書くと無限ループになって動かなくなるので、try,catchと、if elseにしてみた。幼稚なプログラムだが、目的は達成できた。オーバーライドやオーバーロードを使ってもう少しスマートに書くことを目指したい。

***************

f:id:yasui_swift:20210225051628p:plain

西暦と和暦を調べて干支をみる

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;
using System.Globalization;
//koyomiEto_224
//produced by yasui_swift 2021/2/24

namespace koyomiEto_224
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string genGo;
public string nenYear;
public string getuMonth;
public string nichiDay;

//十干十二支の配列を作る
public string eraEtoSi = new string[12] {
"子(ね)",
"丑(うし)",
"寅(とら)",
"卯(う)",
"辰(たつ)",
  "巳(み)",
"午(うま)",
"未(ひつじ)",
"申(さる)",
"酉(とり)",
"戌(いぬ)",
"亥(い)"
};
public string
eraEtoJikkan = new string[10]{
"庚(かのえ)",
"辛(かのと)",
"壬(みずのえ)",
"癸(みすのと)",
"甲(きのえ)",
"乙(きのと)",
"丙(ひのえ)",
"丁(ひのと)",
"戊(つちのえ)",
"己(つちのと)"
};
private void warekiAgeClicked_Click(object sender, EventArgs e)
{
//DateTimeを使う
CultureInfo culture = new CultureInfo("ja-JP", true);
culture.DateTimeFormat.Calendar = new JapaneseCalendar();

DateTime today = DateTime.Today; //今日の日付

//例外処理をtry,catchで行う
try
{
genGo = comboBox1.Text;   //元号
nenYear = textBox1.Text; //年
getuMonth = textBox2.Text; //月
nichiDay = textBox3.Text;  //日

string nenGappi = genGo + nenYear + "/" + getuMonth + "/" + nichiDay;
DateTime finalDate = DateTime.Parse(nenGappi, culture.DateTimeFormat);

//誕生日の日付から年齢を計算する

int ageOld = today.Year - finalDate.Year;//満年齢の計算
if (finalDate > today.AddYears(-ageOld)) //年齢の修正
{
ageOld--;
}

textBox4.Text = finalDate.ToString("yyyy年M月dd日");//変換後の西暦
textBox6.Text = ("満 " + ageOld.ToString()); //年齢

int year = finalDate.Year;//干支の計算
int i = year % 10;
int j = (year + 8) % 12;

string etoJikkan = eraEtoJikkan[i];
string etoSi = eraEtoSi[j];

textBox5.Text = (etoJikkan + etoSi);
}
catch
{
MessageBox.Show(" ⇒ 入力に誤りがあります");
return;
}
}
private void seirekiAgeClicked_Click(object sender, EventArgs e)
{
//日付の入力と計算.西暦から
var ci = new CultureInfo("ja-JP");
ci.DateTimeFormat.Calendar = new JapaneseCalendar();

string strTime;
DateTime dateTime;
strTime = textBox7.Text; // 様式はyyyy/MM/dd or yyyy,MM,ddどちらも可

if (DateTime.TryParse(strTime, out dateTime))
{
string warekiDate = dateTime.ToString("ggyy年MM月dd日", ci);
textBox8.Text = warekiDate;
}
else  //例外処理をif,elseで行う
{
textBox8.Text =( "変換できません!");
}
}
private void button1_Click(object sender, EventArgs e)
{

//日付の入力と計算.西暦から
string strTime;
DateTime dateTime;

var ci = new CultureInfo("ja-JP");
ci.DateTimeFormat.Calendar = new JapaneseCalendar();

strTime = textBox7.Text; // 様式はyyyy/MM/dd or yyyy,MM,ddどちらも可

if (DateTime.TryParse(strTime, out dateTime))
{
string warekiDate = dateTime.ToString("ggyy年MM月dd日", ci);
textBox8.Text = warekiDate;
}
else
{
textBox8.Text = ("変換できません");
}
}
private void Form1_Load(object sender, EventArgs e)
{ }
}
}