Input for WinForms
独自の年号の定義
C1Input コントロールの使い方 > 日本語特有の機能 > 和暦表示(C1DateEdit コントロール) > 独自の年号の定義

C1DateEditの和暦表示に使用される年号を、独自に設定することができます。既定で定義されている年号(明治/大正/昭和/平成)以外を利用したい場合に便利です。

実装方法として、コードでJapaneseEraHelperクラスのAddJapaneseEraメソッドを使用して設定する方法と、アプリケーション構成ファイル(.config)を使用する方法の2通りが可能です。これらは、編集時/表示時のいずれの場合にも使用することができます。

JapaneseEraHelperクラスのAddJapaneseEraメソッドを使用

AddJapaneseEraメソッドは、日本の暦に年号を追加するための関数です。各種パラメータを指定して、独自の年号を設定できます。

AddJapaneseEraメソッド

public static void AddJapaneseEra(int era, System.DateTime startTime, System.DateTime endTime, string eraName, string abbreviatedEraName, string englishEraName, string symbolEraName)

パラメータ 説明
era 年号の数値 ID。
startTime 年号の開始日。
endTime 年号の終了日。
eraName 年号の日本語名。
abbreviatedEraName 年号の日本語省略名。
englishEraName 英語の年号。オプション。
symbolEraName 1 文字の英字による年号。

以下に、AddJapaneseEraメソッドを使用して独自の年号を定義するサンプルコードを紹介します。

Visual Basic
コードのコピー

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        C1DateEdit1.EditFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar
        C1DateEdit1.DisplayFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar
        C1DateEdit1.EditFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat
        C1DateEdit1.DisplayFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat
        C1DateEdit1.EditFormat.CustomFormat = "ggyy年MM月dd日"
        C1DateEdit1.DisplayFormat.CustomFormat = "gyy年MM月dd日"

        C1.Globalization.JapaneseEraHelper.ClearJapaneseEras()
        C1.Globalization.JapaneseEraHelper.AddJapaneseEra(1, New DateTime(1868, 9, 8), New DateTime(1912, 7, 30).AddTicks(-1), "明治", "明", "Meiji", "M")
        C1.Globalization.JapaneseEraHelper.AddJapaneseEra(2, New DateTime(1912, 7, 30), New DateTime(1926, 12, 25).AddTicks(-1), "大正", "大", "Taisho", "T")
        C1.Globalization.JapaneseEraHelper.AddJapaneseEra(3, New DateTime(1926, 12, 25), New DateTime(1989, 1, 8).AddTicks(-1), "昭和", "昭", "Showa", "S")
        C1.Globalization.JapaneseEraHelper.AddJapaneseEra(4, New DateTime(1989, 1, 8), New DateTime(2019, 5, 1).AddTicks(-1), "平成", "平", "Heisei", "H")
        C1.Globalization.JapaneseEraHelper.AddJapaneseEra(5, New DateTime(2019, 5, 1), New DateTime(2100, 1, 1).AddTicks(-1), "令和", "令", "Reiwa", "R")
        C1.Globalization.JapaneseEraHelper.AddJapaneseEra(6, New DateTime(2100, 1, 1), New DateTime(2150, 1, 1).AddTicks(-1), "新規", "新", "New", "N")

        C1DateEdit1.Text = "2100/08/31"
    End Sub

C#
コードのコピー

private void Form1_Load(object sender, EventArgs e)
        {
            c1DateEdit1.EditFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar;
            c1DateEdit1.DisplayFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar;
            c1DateEdit1.EditFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat;
            c1DateEdit1.DisplayFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat;
            c1DateEdit1.EditFormat.CustomFormat = "ggyy年MM月dd日";
            c1DateEdit1.DisplayFormat.CustomFormat = "gyy年MM月dd日";

            C1.Globalization.JapaneseEraHelper.ClearJapaneseEras();
            C1.Globalization.JapaneseEraHelper.AddJapaneseEra(1, new DateTime(1868, 9, 8), new DateTime(1912, 7, 30).AddTicks(-1), "明治", "明", "Meiji", "M");
            C1.Globalization.JapaneseEraHelper.AddJapaneseEra(2, new DateTime(1912, 7, 30), new DateTime(1926, 12, 25).AddTicks(-1), "大正", "大", "Taisho", "T");
            C1.Globalization.JapaneseEraHelper.AddJapaneseEra(3, new DateTime(1926, 12, 25), new DateTime(1989, 1, 8).AddTicks(-1), "昭和", "昭", "Showa", "S");
            C1.Globalization.JapaneseEraHelper.AddJapaneseEra(4, new DateTime(1989, 1, 8), new DateTime(2019, 5, 1).AddTicks(-1), "平成", "平", "Heisei", "H");
            C1.Globalization.JapaneseEraHelper.AddJapaneseEra(5, new DateTime(2019, 5, 1), new DateTime(2100, 1, 1).AddTicks(-1), "令和", "令", "Reiwa", "R");
            C1.Globalization.JapaneseEraHelper.AddJapaneseEra(6, new DateTime(2100, 1, 1), new DateTime(2150, 1, 1).AddTicks(-1), "新規", "新", "New", "N");

            c1DateEdit1.Text = "2100/08/31";
        }

上記コードの実行結果

編集時/表示時に、年号がそれぞれ「新規01年08月31日」/「新01年08月31日」のように表示されます。

アプリケーション構成ファイル(.config)を使用

最初に .configに次のように定義を追加します。

.config
コードのコピー
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <GrapeCityEra>
    <add key="1868 09 08" value="明治_明_M_1,M"/>
    <add key="1912 07 30" value="大正_大_T_2,T"/>
    <add key="1926 12 25" value="昭和_昭_S_3,S"/>
    <add key="1989 01 08" value="平成_平_H_4,H"/>
    <add key="2019 05 01" value="令和_令_R_5,H"/>
    <add key="2100 01 01" value="新規_新_N_6,N"/>
  </GrapeCityEra>
</configuration>

次いで、ReadErasFromGCConfigメソッドを使用した以下のようなコードを用いて上記定義を読み込みます。

Visual Basic
コードのコピー

    C1DateEdit1.EditFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar
    C1DateEdit1.DisplayFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar
    C1DateEdit1.EditFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat
    C1DateEdit1.DisplayFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat
    C1DateEdit1.EditFormat.CustomFormat = "ggyy年MM月dd日"
    C1DateEdit1.DisplayFormat.CustomFormat = "gyy年MM月dd日"

    C1.Win.C1Input.JapaneseEraHelper.ReadErasFromGCConfig("GrapeCityEra")

    C1DateEdit1.Text = "2100/08/31"

C#
コードのコピー

     c1DateEdit1.EditFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar;
     c1DateEdit1.DisplayFormat.CalendarType = C1.Win.C1Input.CalendarType.JapaneseCalendar;
     c1DateEdit1.EditFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat;
     c1DateEdit1.DisplayFormat.FormatType = C1.Win.C1Input.FormatTypeEnum.CustomFormat;
     c1DateEdit1.EditFormat.CustomFormat = "ggyy年MM月dd日";
     c1DateEdit1.DisplayFormat.CustomFormat = "gyy年MM月dd日";

     C1.Win.C1Input.JapaneseEraHelper.ReadErasFromGCConfig("GrapeCityEra");
    
     c1DateEdit1.Text = "2100/08/31";

上記コードの実行結果

(A)のケース同様、編集時/表示時に、年号がそれぞれ「新規01年08月31日」/「新01年08月31日」のように表示されます。

注意: この方法では、フォーム上のすべてのC1Inputコントロールに独自の和暦が適用されます。コントロールごとに設定することはできません。