ディジタル時計の作成
Create Digital Clock
デザイン
ツールボックスの中で、
- –コモンコントロール「TextBox」
- –コンポーネント「Timer」
Formにドラッグ&ドロップする
Timerのプロパティ(値)を変更
timer1のプロパティ
- Enabled⇒ True (Enabled:タイマーを実行する)
- Interval⇒ 1000 (Interval:タイマーの実行間隔, 1000⇒1000ms(1秒))
TextBoxのプロパティ
- Font :36ポイント –文字のサイズ⇒好きなサイズに
- ForeColor :緑 –文字の色⇒好きな色に
- BackColor :ブラック –TextBoxの背景色⇒好きな色に
- TextAlign :Center –文字を表示する場所⇒真ん中(Center)
- Text: 00:00:00 –表示する文字列⇒最初は00時00分00秒をあらわす
ディジタル時計の完成
timer1をダブルクリックすると、timer処理に関する関数(メソッド)が自動生成される
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;
namespace DigitalClock
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToLongTimeString();
}
}
}
現在の時間をテキストボックスに表示コードの追加