C#を使ったデジタル時計の作成の仕方について徹底解説!

- システム
エンジニア - デジタル時計の作成に興味があります。
- プロジェクト
マネージャー - C#だと簡単に作成できますよ。実際のソースコードを参考に見ていきましょう。
C#の時計について
今回は、C#の時計についてご説明していきます。
TimerクラスのTickイベントを利用すれば、簡単にデジタル時計を作成することができます。C#の時計に興味のある方は、ぜひ参考にしてみてはいかがでしょうか。
デジタル時計
ここでは、C#を使ったデジタル時計の作成方法をご紹介していきます。
時刻表示機能、時刻設定機能、アラーム機能について、順番に解説していきますので、機能を応用して、自分なりに拡張してみましょう。
時刻表示機能
C#で、TimerクラスのTickイベントを利用して、時刻表示機能があるデジタル時計を作成してみます。
実際のソースコードを見てみましょう。
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Timer timer; Label timeLabel; DateTime dt; public Form1() { InitializeComponent(); this.Text = "デジタル時計"; this.Size = new Size(300, 300); this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { timeLabel = new Label(); timeLabel.Location = new Point(10, 10); timeLabel.AutoSize = true; dt = DateTime.Now; timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); // timerの設定 timer = new Timer(); timer.Interval = 1000; timer.Enabled = true; timer.Tick += Timer_Tick; this.Controls.Add(timeLabel); } // 1秒周期のtimerイベント private void Timer_Tick(object sender, EventArgs e) { dt = DateTime.Now; timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); } } } |
1000ミリ秒(1秒)周期のtimerイベントで現在時刻を取得し、labelを更新しています。
1秒ごとに、時刻が更新されることが分かります。
このように、C#で、TimerクラスのTickイベントを利用してデジタル時計を作成できます。
時刻設定機能
先ほどのサンプルでは、現在時刻を表示するだけでしたが、これを応用して、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 { Timer timer; Label timeLabel; DateTime dt; Button timeChangeButton, timeChangeOKButton; Form timeChangeForm; DateTimePicker dateTimePicker1; TimeSpan timeSpan; public Form1() { InitializeComponent(); this.Text = "デジタル時計"; this.Size = new Size(300, 300); this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { timeLabel = new Label(); timeLabel.Location = new Point(10, 10); timeLabel.AutoSize = true; dt = DateTime.Now; timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); timer = new Timer(); timer.Interval = 1000; timer.Enabled = true; timer.Tick += Timer_Tick; timeChangeButton = new Button(); timeChangeButton.Location = new Point(10, 40); timeChangeButton.Text = "時刻設定"; timeChangeButton.Click += TimeChangeButton_Click; // 時刻加算用のTimeSpan timeSpan = new TimeSpan(0, 0, 0, 1); // 時刻設定Form ///////////////////////// timeChangeForm = new Form(); timeChangeForm.Text = "時刻設定"; dateTimePicker1 = new DateTimePicker(); dateTimePicker1.Location = new Point(10, 10); dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "yyyy/MM/dd HH:mm:ss"; timeChangeForm.Controls.Add(dateTimePicker1); timeChangeOKButton = new Button(); timeChangeOKButton.Location = new Point(10, 40); timeChangeOKButton.Text = "OK"; timeChangeOKButton.Click += TimeChangeOKButton_Click; timeChangeForm.Controls.Add(timeChangeOKButton); //////////////////////////////////////// this.Controls.Add(timeLabel); this.Controls.Add(timeChangeButton); } private void TimeChangeButton_Click(object sender, EventArgs e) { if (timeChangeForm.ShowDialog(this) == DialogResult.OK) { dt = dateTimePicker1.Value; timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); } } private void TimeChangeOKButton_Click(object sender, EventArgs e) { timeChangeForm.DialogResult = DialogResult.OK; timeChangeForm.Close(); } private void Timer_Tick(object sender, EventArgs e) { // 1秒加算する dt = dt.Add(timeSpan); timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); } } } |
“時刻設定”ボタンをクリックすると、時刻設定Formが開きます。
DateTimePickerクラスで時刻を設定し、OKボタンをクリックすると、設定した日時でデジタル時計が更新されます。
日時を自由に変更してみてください。変更後に、timerイベントで1秒ごとに1秒加算されることが分かります。
このように、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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Timer timer; Label timeLabel, alarmLabel; DateTime dt, alarmDt; Button timeChangeButton, timeChangeOKButton, alarmSetButton, alarmSetOKButton, alarmStopButton; Form timeChangeForm, alarmSetForm; DateTimePicker dateTimePicker1, dateTimePicker2; TimeSpan timeSpan; Boolean isAlarmSet, isAlarmStop; public Form1() { InitializeComponent(); this.Text = "デジタル時計"; this.Size = new Size(300, 300); this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { timeLabel = new Label(); timeLabel.Location = new Point(10, 10); timeLabel.AutoSize = true; dt = DateTime.Now; timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); alarmLabel = new Label(); alarmLabel.Location = new Point(150, 10); alarmLabel.AutoSize = true; alarmLabel.Visible = false; timer = new Timer(); timer.Interval = 1000; timer.Enabled = true; timer.Tick += Timer_Tick; timeChangeButton = new Button(); timeChangeButton.Location = new Point(10, 40); timeChangeButton.Text = "時刻設定"; timeChangeButton.Click += TimeChangeButton_Click; alarmSetButton = new Button(); alarmSetButton.Location = new Point(100, 40); alarmSetButton.Text = "アラーム設定"; alarmSetButton.Click += AlarmSetButton_Click; alarmStopButton = new Button(); alarmStopButton.Location = new Point(190, 40); alarmStopButton.Text = "アラーム停止"; alarmStopButton.Visible = false; alarmStopButton.Click += AlarmStopButton_Click; timeSpan = new TimeSpan(0, 0, 0, 1); isAlarmSet = false; isAlarmStop = false; // 時刻設定Form ///////////////////////// timeChangeForm = new Form(); timeChangeForm.Text = "時刻設定"; dateTimePicker1 = new DateTimePicker(); dateTimePicker1.Location = new Point(10, 10); dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "yyyy/MM/dd HH:mm:ss"; timeChangeForm.Controls.Add(dateTimePicker1); timeChangeOKButton = new Button(); timeChangeOKButton.Location = new Point(10, 40); timeChangeOKButton.Text = "OK"; timeChangeOKButton.Click += TimeChangeOKButton_Click; timeChangeForm.Controls.Add(timeChangeOKButton); //////////////////////////////////////// // アラーム設定Form //////////////////// alarmSetForm = new Form(); alarmSetForm.Text = "アラーム設定"; dateTimePicker2 = new DateTimePicker(); dateTimePicker2.Location = new Point(10, 10); dateTimePicker2.Format = DateTimePickerFormat.Custom; dateTimePicker2.CustomFormat = "yyyy/MM/dd HH:mm:ss"; alarmSetForm.Controls.Add(dateTimePicker2); alarmSetOKButton = new Button(); alarmSetOKButton.Location = new Point(10, 40); alarmSetOKButton.Text = "OK"; alarmSetOKButton.Click += AlarmSetOKButton_Click; alarmSetForm.Controls.Add(alarmSetOKButton); //////////////////////////////////////// this.Controls.Add(timeLabel); this.Controls.Add(alarmLabel); this.Controls.Add(timeChangeButton); this.Controls.Add(alarmSetButton); this.Controls.Add(alarmStopButton); } private void TimeChangeButton_Click(object sender, EventArgs e) { if (timeChangeForm.ShowDialog(this) == DialogResult.OK) { dt = dateTimePicker1.Value; timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); } } private void TimeChangeOKButton_Click(object sender, EventArgs e) { timeChangeForm.DialogResult = DialogResult.OK; timeChangeForm.Close(); } private void AlarmSetButton_Click(object sender, EventArgs e) { if (alarmSetForm.ShowDialog(this) == DialogResult.OK) { alarmDt = dateTimePicker2.Value; isAlarmSet = true; isAlarmStop = false; alarmLabel.Text = string.Format("アラーム\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", alarmDt.Year, alarmDt.Month, alarmDt.Day, alarmDt.Hour, alarmDt.Minute, alarmDt.Second); alarmLabel.Visible = true; } } private void AlarmSetOKButton_Click(object sender, EventArgs e) { alarmSetForm.DialogResult = DialogResult.OK; alarmSetForm.Close(); } private void AlarmStopButton_Click(object sender, EventArgs e) { isAlarmStop = true; alarmLabel.Visible = false; alarmStopButton.Visible = false; Console.WriteLine("alarm stopped"); } private void Timer_Tick(object sender, EventArgs e) { dt = dt.Add(timeSpan); timeLabel.Text = string.Format("日時\r\n{0:0000}/{1:00}/{2:00} {3:00}:{4:00}:{5:00}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); if (isAlarmSet && dt >= alarmDt && !isAlarmStop) { Console.WriteLine("alarm!!!!"); alarmStopButton.Visible = true; } else { alarmStopButton.Visible = false; } } } } |
“アラーム設定”ボタンをクリックすると、アラーム設定画面が開き、アラームを設定すると、アラーム設定時刻がデジタル時計に表示されます。
アラームの時間を過ぎると、アラームが発生します。
サンプルプログラムでは、コンソールに”alarm!!!!”が表示されます。
“アラーム停止”ボタンをクリックすると、標準出力が停止されます。このように、C#でアラーム機能を実装できます。
- システム
エンジニア - アラーム機能が実装できたり、いろいろカスタマイズできそうですね。
- プロジェクト
マネージャー - 興味が深まったようで何よりです。是非実際に作成してみてください。
C#を使ってデジタル時計を作成してみよう
いかがでしたでしょうか。C#では、TimerクラスのTickイベントを利用すれば、簡単にデジタル時計を作成できます。ここで紹介した機能を応用して、自分なりに拡張してみましょう。
今回はデジタル時計を紹介しましたが、長針や短針を描画すればアナログ時計も作成できます。ぜひご自身で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万円東京都新宿区(西新宿駅)