C#のカレンダーについて解説!DateTimePickerでのカレンダー作成方法

- システム
エンジニア - C#でカレンダー作成ができるのですか?
- プロジェクト
マネージャー - もちろんできます。フォーマットもありますので詳しく見ていきましょう。
C#のカレンダーについて
C#でカレンダーを扱うクラスには、DateTimePickerとMonthCalendarがあります。
ここでは、DateTimePickerについて紹介します。C#のカレンダーに興味のある方はぜひご覧ください。
カレンダーの作成
C#ではDateTimePickerでカレンダーを作成でき、カレンダーのフォーマットは「Long」「Short」「Time」「Custom」の4種類があります。
既定のフォーマットは「Long」で、「Custom」フォーマットでは、フォーマットをカスタマイズできます。
実際のソースコードを見てみましょう。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Label label1, label2, label3, label4;
DateTimePicker dateTimePicker1, dateTimePicker2, dateTimePicker3, dateTimePicker4;
public Form1()
{
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
// Longフォーマットのカレンダー
label1 = new Label();
label1.Location = new Point(10, 10);
label1.Text = "Longフォーマット";
dateTimePicker1 = new DateTimePicker();
dateTimePicker1.Location = new Point(10, 30);
dateTimePicker1.Format = DateTimePickerFormat.Long;
// Shortフォーマットのカレンダー
label2 = new Label();
label2.Location = new Point(220, 10);
label2.Text = "Shortフォーマット";
dateTimePicker2 = new DateTimePicker();
dateTimePicker2.Location = new Point(220, 30);
dateTimePicker2.Format = DateTimePickerFormat.Short;
// Timeフォーマットのカレンダー
label3 = new Label();
label3.Location = new Point(430, 10);
label3.Text = "Timeフォーマット";
dateTimePicker3 = new DateTimePicker();
dateTimePicker3.Location = new Point(430, 30);
dateTimePicker3.Format = DateTimePickerFormat.Time;
// Customフォーマットのカレンダー
label4 = new Label();
label4.Location = new Point(640, 10);
label4.Text = "Customフォーマット";
dateTimePicker4 = new DateTimePicker();
dateTimePicker4.Location = new Point(640, 30);
dateTimePicker4.Format = DateTimePickerFormat.Custom;
dateTimePicker4.CustomFormat = "yyyy/MM/dd HH:mm:ss";
this.Controls.Add(dateTimePicker1);
this.Controls.Add(dateTimePicker2);
this.Controls.Add(dateTimePicker3);
this.Controls.Add(dateTimePicker4);
this.Controls.Add(label1);
this.Controls.Add(label2);
this.Controls.Add(label3);
this.Controls.Add(label4);
this.AutoSize = true;
}
}
}
|
実行すると、4種類のカレンダーが表示されます。カレンダーを操作してみてください。
カレンダーのプロパティ
C#のDateTimePickerの主なプロパティを紹介します。
・MinDate
カレンダの最小値を設定できます。
・MaxDate
カレンダの最大値を設定できます。
・ShowUpDown
アップダウンコントロールで日付や時刻値を設定できます。
・ShowCheckBox
選択した日付の左側にチェックボックスを表示できます。
次の章でこれらのプロパティを設定したソースコードを説明します。
カレンダーの値取得
カレンダーに設定した値を取得できます。
「Get」ボタンをクリックすると、4種類のフォーマットのカレンダ値を取得します。
実際のソースコードを見てみましょう。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Label label1, label2, label3, label4;
DateTimePicker dateTimePicker1, dateTimePicker2, dateTimePicker3, dateTimePicker4;
Button button;
public Form1()
{
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
label1 = new Label();
label1.Location = new Point(10, 10);
label1.Text = "Longフォーマット";
dateTimePicker1 = new DateTimePicker();
dateTimePicker1.Location = new Point(10, 30);
dateTimePicker1.Format = DateTimePickerFormat.Long;
dateTimePicker1.MinDate = DateTime.Today; // カレンダ最小値の設定
label2 = new Label();
label2.Location = new Point(220, 10);
label2.Text = "Shortフォーマット";
dateTimePicker2 = new DateTimePicker();
dateTimePicker2.Location = new Point(220, 30);
dateTimePicker2.Format = DateTimePickerFormat.Short;
dateTimePicker1.MaxDate = new DateTime(2021, 12, 31); // カレンダ最大値の設定
label3 = new Label();
label3.Location = new Point(430, 10);
label3.Text = "Timeフォーマット";
dateTimePicker3 = new DateTimePicker();
dateTimePicker3.Location = new Point(430, 30);
dateTimePicker3.Format = DateTimePickerFormat.Time;
dateTimePicker3.ShowUpDown = true; // アップダウンコントロールにする
label4 = new Label();
label4.Location = new Point(640, 10);
label4.Text = "Customフォーマット";
dateTimePicker4 = new DateTimePicker();
dateTimePicker4.Location = new Point(640, 30);
dateTimePicker4.Format = DateTimePickerFormat.Custom;
dateTimePicker4.CustomFormat = "yyyy/MM/dd HH:mm:ss";
dateTimePicker4.ShowCheckBox = true; // 選択した日付の左側にチェックボックスを表示
button = new Button();
button.Location = new Point(10, 220);
button.Text = "Get";
button.Click += Button_Click;
this.Controls.Add(dateTimePicker1);
this.Controls.Add(dateTimePicker2);
this.Controls.Add(dateTimePicker3);
this.Controls.Add(dateTimePicker4);
this.Controls.Add(label1);
this.Controls.Add(label2);
this.Controls.Add(label3);
this.Controls.Add(label4);
this.Controls.Add(button);
this.AutoSize = true;
}
// 4種類のフォーマットのカレンダーから値を取得
private void Button_Click(object sender, EventArgs e)
{
Console.WriteLine(dateTimePicker1.Value.ToLongDateString());
Console.WriteLine(dateTimePicker2.Value.ToLongTimeString());
Console.WriteLine(dateTimePicker3.Value.ToShortDateString());
Console.WriteLine(dateTimePicker4.Value.ToShortTimeString());
}
}
}
|
「Get」ボタンをクリックすると、4種類のフォーマットのカレンダから値を取得していることが分かります。
それぞれ、異なる形式で値を取得しています。
カレンダの値を自由に変更して「Get」ボタンをクリックしてみてください。
このように、C#ではカレンダーに設定した値を取得できます。
カレンダーの変更イベント
C#では、カレンダ値の変更イベントを取得できます。
実際のソースコードを見てみましょう。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Label label1, label2, label3, label4;
DateTimePicker dateTimePicker1, dateTimePicker2, dateTimePicker3, dateTimePicker4;
Button button;
public Form1()
{
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
label1 = new Label();
label1.Location = new Point(10, 10);
label1.Text = "Longフォーマット";
dateTimePicker1 = new DateTimePicker();
dateTimePicker1.Location = new Point(10, 30);
dateTimePicker1.Format = DateTimePickerFormat.Long;
dateTimePicker1.MinDate = DateTime.Today;
dateTimePicker1.ValueChanged += DateTimePicker_ValueChanged; // イベントハンドラの設定
label2 = new Label();
label2.Location = new Point(220, 10);
label2.Text = "Shortフォーマット";
dateTimePicker2 = new DateTimePicker();
dateTimePicker2.Location = new Point(220, 30);
dateTimePicker2.Format = DateTimePickerFormat.Short;
dateTimePicker1.MaxDate = new DateTime(2021, 12, 31);
dateTimePicker2.ValueChanged += DateTimePicker_ValueChanged; // イベントハンドラの設定
label3 = new Label();
label3.Location = new Point(430, 10);
label3.Text = "Timeフォーマット";
dateTimePicker3 = new DateTimePicker();
dateTimePicker3.Location = new Point(430, 30);
dateTimePicker3.Format = DateTimePickerFormat.Time;
dateTimePicker3.ShowUpDown = true;
dateTimePicker3.ValueChanged += DateTimePicker_ValueChanged; // イベントハンドラの設定
label4 = new Label();
label4.Location = new Point(640, 10);
label4.Text = "Customフォーマット";
dateTimePicker4 = new DateTimePicker();
dateTimePicker4.Location = new Point(640, 30);
dateTimePicker4.Format = DateTimePickerFormat.Custom;
dateTimePicker4.CustomFormat = "yyyy/MM/dd HH:mm:ss";
dateTimePicker4.ShowCheckBox = true;
dateTimePicker4.ValueChanged += DateTimePicker_ValueChanged; // イベントハンドラの設定
button = new Button();
button.Location = new Point(10, 220);
button.Text = "Get";
button.Click += Button_Click;
this.Controls.Add(dateTimePicker1);
this.Controls.Add(dateTimePicker2);
this.Controls.Add(dateTimePicker3);
this.Controls.Add(dateTimePicker4);
this.Controls.Add(label1);
this.Controls.Add(label2);
this.Controls.Add(label3);
this.Controls.Add(label4);
this.Controls.Add(button);
this.AutoSize = true;
}
private void Button_Click(object sender, EventArgs e)
{
Console.WriteLine(dateTimePicker1.Value.ToLongDateString());
Console.WriteLine(dateTimePicker2.Value.ToLongTimeString());
Console.WriteLine(dateTimePicker3.Value.ToShortDateString());
Console.WriteLine(dateTimePicker4.Value.ToShortTimeString());
}
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
// イベントが発生したDateTimePickerを取得
DateTimePicker dateTimePicker = (DateTimePicker)sender;
// 設定値を表示
Console.WriteLine(dateTimePicker.Value.ToString());
}
}
}
|
カレンダの値を変更すると、変更値が標準出力されることが分かります。
このように、C#ではカレンダ値の変更イベントを取得できます。
- システム
エンジニア - 2種類のクラスに4種類のフォーマットがあるんですね。
- プロジェクト
マネージャー - フォーマットのカスタマイズもできますので、実際に書いて理解を深めてみてください。
まとめ
いかがでしたでしょうか。C#でカレンダーを扱うクラスには、DateTimePickerとMonthCalendarがあります。ここでは、DateTimePickerについて紹介しました。ぜひご自身でC#のソースコードを書いて、理解を深めてください。
FEnet.NETナビ・.NETコラムは株式会社オープンアップシステムが運営しています。
株式会社オープンアップシステムはこんな会社です
秋葉原オフィスには株式会社オープンアップシステムをはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
スマホアプリから業務系システムまで
スマホアプリから業務系システムまで開発案件多数。システムエンジニア・プログラマーとしての多彩なキャリアパスがあります。
-
充実した研修制度
毎年、IT技術のトレンドや社員の要望に合わせて、カリキュラムを刷新し展開しています。社内講師の丁寧なサポートを受けながら、自分のペースで学ぶことができます。
-
資格取得を応援
スキルアップしたい社員を応援するために資格取得一時金制度を設けています。受験料(実費)と合わせて資格レベルに合わせた最大10万円の一時金も支給しています。
-
東証プライム上場企業グループ
オープンアップシステムは東証プライム上場「株式会社夢真ビーネックスグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
株式会社オープンアップシステムに興味を持った方へ
株式会社オープンアップシステムでは、開発系エンジニア・プログラマを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
まずは話だけでも聞いてみたい場合もOK。お気軽にご登録ください。


C#新着案件New Job
-
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(新宿駅) -
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(新宿駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円神奈川県横浜市(桜木町駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円神奈川県横浜市(桜木町駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(西新宿駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(西新宿駅)