C# exercises (3) Common Control

ディジタル時計の作成

Create Digital Clock

Contents

デザイン

ツールボックスの中で、

  • –コモンコントロール「TextBox」
  • –コンポーネント「Timer」

Formにドラッグ&ドロップする

スクリーンショット 2016-05-02 15.27.53

Timerのプロパティ(値)を変更

スクリーンショット 2016-05-02 15.28.49

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秒をあらわす

 

スクリーンショット 2016-05-02 15.34.31

 

ディジタル時計の完成

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();
        }
    }
}

 

現在の時間をテキストボックスに表示コードの追加

スクリーンショット 2016-05-02 15.44.58