Home > C# Archive

C# Archive

C#でキッチンタイマーを作ろう

カウントダウンタイマーとも言うのかな。
さて、今回学んだことは、

  • X秒からh:m:s形式での表示。
  • タイマースレッドの利用。

かな。
タイマーイベント毎に重い処理を行うと表示時間と実時間のずれが大きくなるので注意。そういった場合はイベント発生間隔を長めに取ってみる。

プログラムの実行結果

起動時

キッチンタイマー初期画面 by yukun

実行時

キッチンタイマーのカウント時 by yukun

ちなみにボタンをロックするにはボタンインスタンスのEnabledプロパティにfalseを代入します(ロック解除はture)。

例(butstartはボタンクラスのインスタンス変数):

butstart.Enabled = false; // スタートボタンをロック(スタートが押されたら場合)

ソースコード

FormTimer.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
/**
 * キッチンタイマー
 * Web page: http://www.yukun.info/
 * license GPLv2
 */
namespace Sample
{
  public partial class FormTimer : Form
  {
    public FormTimer()
    {
      InitializeComponent();
      // マウスポインタの場所に表示
      this.DesktopLocation = new Point(System.Windows.Forms.Cursor.Position.X,
        System.Windows.Forms.Cursor.Position.Y);
    }
    int sec = 0; // 計測時間

    private void viewtime()
    {
      stLabel1.Text = "" + sec / 36000 % 10 + sec / 3600 % 10 +
                     ":" + sec / 600 % 6 + sec / 60 % 10 +
                     ":" + sec / 10 % 6 + sec % 10;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      sec--;
      if (0 == sec)
      {
        sttimer.Enabled = false;
        System.Media.SystemSounds.Beep.Play();
        this.Activate();
      }
      viewtime();
    }

    private void butsec_Click(object sender, EventArgs e)
    {
      sec += 10;
      viewtime();
    }

    private void butmin_Click(object sender, EventArgs e)
    {
      sec += 60;
      viewtime();
    }

    private void buthour_Click(object sender, EventArgs e)
    {
      sec += 3600;
      if (sec >= 360000) sec = 0;
      viewtime();
    }

    private void butstart_Click(object sender, EventArgs e)
    {
      if (0 == sec) return;
      sttimer.Enabled = true;
      this.butstop.Enabled = true;
      this.butstart.Enabled = false;
      this.buthour.Enabled = false;
      this.butmin.Enabled = false;
      this.butsec.Enabled = false;
      this.butreset.Enabled = false;
    }

    private void butstop_Click(object sender, EventArgs e)
    {
      sttimer.Enabled = false;
      this.butstop.Enabled = false;
      this.butstart.Enabled = true;
      this.buthour.Enabled = true;
      this.butmin.Enabled = true;
      this.butsec.Enabled = true;
      this.butreset.Enabled = true;
    }

    private void butreset_Click(object sender, EventArgs e)
    {
      stLabel1.Text = "00:00:00";
      sec = 0;
    }

    private void 常に手前に表示ToolStripMenuItem_Click(object sender, EventArgs e)
    {
      //クリックするごとにこのフォームを常に手前または解除します。
      this.TopMost = !this.TopMost;
    }
  }
}

FormTimer.Designer.cs

namespace Sample
{
  partial class FormTimer
  {
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 使用中のリソースをすべてクリーンアップします。
    /// </summary>
    /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows フォーム デザイナで生成されたコード

    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container();
      this.sttimer = new System.Windows.Forms.Timer(this.components);
      this.buthour = new System.Windows.Forms.Button();
      this.butmin = new System.Windows.Forms.Button();
      this.butsec = new System.Windows.Forms.Button();
      this.butstart = new System.Windows.Forms.Button();
      this.stLabel1 = new System.Windows.Forms.Label();
      this.butstop = new System.Windows.Forms.Button();
      this.butreset = new System.Windows.Forms.Button();
      this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
      this.常に手前に表示ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
      this.contextMenuStrip1.SuspendLayout();
      this.SuspendLayout();
      //
      // sttimer
      //
      this.sttimer.Interval = 1000;
      this.sttimer.Tick += new System.EventHandler(this.timer1_Tick);
      //
      // buthour
      //
      this.buthour.Location = new System.Drawing.Point(6, 66);
      this.buthour.Name = "buthour";
      this.buthour.Size = new System.Drawing.Size(50, 23);
      this.buthour.TabIndex = 0;
      this.buthour.Text = "HOUR";
      this.buthour.UseVisualStyleBackColor = true;
      this.buthour.Click += new System.EventHandler(this.buthour_Click);
      //
      // butmin
      //
      this.butmin.Location = new System.Drawing.Point(62, 66);
      this.butmin.Name = "butmin";
      this.butmin.Size = new System.Drawing.Size(50, 23);
      this.butmin.TabIndex = 1;
      this.butmin.Text = "MIN";
      this.butmin.UseVisualStyleBackColor = true;
      this.butmin.Click += new System.EventHandler(this.butmin_Click);
      //
      // butsec
      //
      this.butsec.Location = new System.Drawing.Point(118, 66);
      this.butsec.Name = "butsec";
      this.butsec.Size = new System.Drawing.Size(50, 23);
      this.butsec.TabIndex = 2;
      this.butsec.Text = "SEC";
      this.butsec.UseVisualStyleBackColor = true;
      this.butsec.Click += new System.EventHandler(this.butsec_Click);
      //
      // butstart
      //
      this.butstart.Location = new System.Drawing.Point(6, 100);
      this.butstart.Name = "butstart";
      this.butstart.Size = new System.Drawing.Size(50, 23);
      this.butstart.TabIndex = 3;
      this.butstart.Text = "スタート";
      this.butstart.UseVisualStyleBackColor = true;
      this.butstart.Click += new System.EventHandler(this.butstart_Click);
      //
      // stLabel1
      //
      this.stLabel1.AutoSize = true;
      this.stLabel1.BackColor = System.Drawing.SystemColors.Control;
      this.stLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.stLabel1.Font = new System.Drawing.Font("Times New Roman", 30F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
      this.stLabel1.Location = new System.Drawing.Point(6, 9);
      this.stLabel1.Name = "stLabel1";
      this.stLabel1.Size = new System.Drawing.Size(163, 42);
      this.stLabel1.TabIndex = 4;
      this.stLabel1.Text = "00:00:00";
      //
      // butstop
      //
      this.butstop.Location = new System.Drawing.Point(62, 100);
      this.butstop.Name = "butstop";
      this.butstop.Size = new System.Drawing.Size(50, 23);
      this.butstop.TabIndex = 5;
      this.butstop.Text = "ストップ";
      this.butstop.UseVisualStyleBackColor = true;
      this.butstop.Click += new System.EventHandler(this.butstop_Click);
      //
      // butreset
      //
      this.butreset.Location = new System.Drawing.Point(118, 100);
      this.butreset.Name = "butreset";
      this.butreset.Size = new System.Drawing.Size(50, 23);
      this.butreset.TabIndex = 6;
      this.butreset.Text = "リセット";
      this.butreset.UseVisualStyleBackColor = true;
      this.butreset.Click += new System.EventHandler(this.butreset_Click);
      //
      // contextMenuStrip1
      //
      this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
      this.常に手前に表示ToolStripMenuItem});
      this.contextMenuStrip1.Name = "contextMenuStrip1";
      this.contextMenuStrip1.Size = new System.Drawing.Size(171, 26);
      //
      // 常に手前に表示ToolStripMenuItem
      //
      this.常に手前に表示ToolStripMenuItem.CheckOnClick = true;
      this.常に手前に表示ToolStripMenuItem.Name = "常に手前に表示ToolStripMenuItem";
      this.常に手前に表示ToolStripMenuItem.Size = new System.Drawing.Size(170, 22);
      this.常に手前に表示ToolStripMenuItem.Text = "常に Top に表示する";
      this.常に手前に表示ToolStripMenuItem.Click += new System.EventHandler(this.常に手前に表示ToolStripMenuItem_Click);
      //
      // FormTimer
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(180, 129);
      this.ContextMenuStrip = this.contextMenuStrip1;
      this.Controls.Add(this.butreset);
      this.Controls.Add(this.butstop);
      this.Controls.Add(this.stLabel1);
      this.Controls.Add(this.butstart);
      this.Controls.Add(this.butsec);
      this.Controls.Add(this.butmin);
      this.Controls.Add(this.buthour);
      this.Name = "FormTimer";
      this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
      this.Text = "タイマー";
      this.contextMenuStrip1.ResumeLayout(false);
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Button buthour;
    private System.Windows.Forms.Button butmin;
    private System.Windows.Forms.Button butsec;
    private System.Windows.Forms.Button butstart;
    private System.Windows.Forms.Label stLabel1;
    private System.Windows.Forms.Button butstop;
    private System.Windows.Forms.Button butreset;
    private System.Windows.Forms.Timer sttimer;
    private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
    private System.Windows.Forms.ToolStripMenuItem 常に手前に表示ToolStripMenuItem;
  }
}

イースト辞書Webサービスを利用したC#(with .Net)クライアントプログラミング

C#からWebサービスを扱う練習をしてみました。例として、イースト辞書Webサービスを利用しようと思い宇宙仮面の C# プログラミングこちらのページのソースコードを参考にしました(謝々)。
SOAP版APIの最新バージョンがv10になり、仕様が変更になったので以下にそれに対応したソースコードを示します。

実行結果

イースト辞書Webサービスを利用したクライアントソフト by yukun

ソースコード

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using netdictist.jp.co.est.btonic; // Webサービスの名前空間を追加

namespace netdictist
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private NetDicV10 netDicV10 = null; // Web Service インスタンス格納用変数
    private DicInfo[] dicInfoList = null; // 辞書情報の配列(出力)
    private DicInfo currentDict = null; // 辞書
    private DicItem[] itemList = null; // 辞書項目の配列(出力)

    private void Form1_Load(object sender, EventArgs e)
    {
      #region ComboBox に辞書リストを設定する。

      // Web辞書検索サービスのインスタンスを作成する。
      this.netDicV10 = new NetDicV10();
      // 辞書のリストを取得する。
      dicInfoList = netDicV10.GetDicList("");
      foreach (DicInfo dicInfo in dicInfoList)
      {
        // comboBox1に項目を追加
        int index = this.comboBox1.Items.Add(dicInfo.FullName);
        if (index == 2) break;
      }
      this.comboBox1.SelectedIndex = 0;

      #endregion
    }

    // 辞書が変更になった。
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      this.currentDict = this.dicInfoList[this.comboBox1.SelectedIndex];
    }

    private void button1_Click(object sender, EventArgs e)
    {
      DicInfo d = this.currentDict; // 選択された辞書
      uint reqItemIndex = 0; // 取得する辞書項目の開始インデックス
      uint reqItemTitleCount = 10; // 取得する辞書項目の数
      uint reqItemContentCount = 10; // 内容も同時に取得する辞書項目の数
      uint itemCountTotal;  // 見つかった辞書項目数(出力)

      Cursor cursor = this.Cursor;
      this.Cursor = Cursors.WaitCursor; // Wait Cursor にする。
      Query[] qw = new Query[1]; // クエリ構造体
      qw[0] = new Query();
      qw[0].Words = this.tbSearchText.Text;
      qw[0].ScopeID = d.ScopeList[0].ID;
      //グローバル一意識別子(GUID)の作成
      Guid[] dicGuid = new Guid[1];
      dicGuid[0] = System.Guid.NewGuid();
      dicGuid[0] = d.DicID;
      ContentProfile cp = new ContentProfile();
      cp.CharsetOption = CharsetOption.UNICODE; // 使用文字セット指定
      cp.FormatType = "XHTML";
      cp.ResourceOption = ResourceOption.URI;

      itemCountTotal = netDicV10.SearchDicItem(
        "", // 認証チケット文字列
        dicGuid, // 辞書ID
        qw, // 検索語(クエリ構造体)
        cp, // ContentProfile構造体
        "", // ソート用(使用しない)
        reqItemIndex,  // 取得する辞書項目の開始インデックス
        reqItemTitleCount, // 取得する辞書項目の数
        reqItemContentCount, // 内容も同時に取得する辞書項目の数
        out itemList  // 辞書項目の配列(出力)
        );
      this.Cursor = cursor;   // Cursor を元に戻す。

      this.labelMessage.Text =
        itemCountTotal.ToString() +
          "件みつかりました。最大10件まで表示します。";
      this.listBox1.Items.Clear();
      foreach (DicItem dicItem in itemList)
      {
        this.listBox1.Items.Add(dicItem.Title.InnerText);
      }
    }

    // 検索結果のリストをセレクトしたので、詳細を表示する。
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      string dictext =
        this.itemList[this.listBox1.SelectedIndex].Body.InnerText;
      this.richTextBox1.Text = dictext;
    }
  }
}

Form1.Designer.cs

namespace netdictist
{
  partial class Form1
  {
    /// <summary>
    /// 必要なデザイナ変数です。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 使用中のリソースをすべてクリーンアップします。
    /// </summary>
    /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows フォーム デザイナで生成されたコード

    /// <summary>
    /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
    /// コード エディタで変更しないでください。
    /// </summary>
    private void InitializeComponent()
    {
      this.comboBox1 = new System.Windows.Forms.ComboBox();
      this.tbSearchText = new System.Windows.Forms.TextBox();
      this.button1 = new System.Windows.Forms.Button();
      this.labelMessage = new System.Windows.Forms.Label();
      this.listBox1 = new System.Windows.Forms.ListBox();
      this.richTextBox1 = new System.Windows.Forms.RichTextBox();
      this.SuspendLayout();
      //
      // comboBox1
      //
      this.comboBox1.FormattingEnabled = true;
      this.comboBox1.Location = new System.Drawing.Point(14, 12);
      this.comboBox1.Name = "comboBox1";
      this.comboBox1.Size = new System.Drawing.Size(204, 20);
      this.comboBox1.TabIndex = 0;
      this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
      //
      // tbSearchText
      //
      this.tbSearchText.Location = new System.Drawing.Point(14, 51);
      this.tbSearchText.Name = "tbSearchText";
      this.tbSearchText.Size = new System.Drawing.Size(204, 19);
      this.tbSearchText.TabIndex = 1;
      //
      // button1
      //
      this.button1.Location = new System.Drawing.Point(14, 87);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(204, 31);
      this.button1.TabIndex = 2;
      this.button1.Text = "検索";
      this.button1.UseVisualStyleBackColor = true;
      this.button1.Click += new System.EventHandler(this.button1_Click);
      //
      // labelMessage
      //
      this.labelMessage.AutoSize = true;
      this.labelMessage.Location = new System.Drawing.Point(12, 136);
      this.labelMessage.Name = "labelMessage";
      this.labelMessage.Size = new System.Drawing.Size(136, 12);
      this.labelMessage.TabIndex = 3;
      this.labelMessage.Text = "最大10件まで表示します。";
      //
      // listBox1
      //
      this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)));
      this.listBox1.FormattingEnabled = true;
      this.listBox1.ItemHeight = 12;
      this.listBox1.Location = new System.Drawing.Point(235, 12);
      this.listBox1.Name = "listBox1";
      this.listBox1.Size = new System.Drawing.Size(160, 136);
      this.listBox1.TabIndex = 4;
      this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
      //
      // richTextBox1
      //
      this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
      this.richTextBox1.Location = new System.Drawing.Point(14, 169);
      this.richTextBox1.Name = "richTextBox1";
      this.richTextBox1.Size = new System.Drawing.Size(381, 241);
      this.richTextBox1.TabIndex = 5;
      this.richTextBox1.Text = "";
      //
      // Form1
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(407, 422);
      this.Controls.Add(this.richTextBox1);
      this.Controls.Add(this.listBox1);
      this.Controls.Add(this.labelMessage);
      this.Controls.Add(this.button1);
      this.Controls.Add(this.tbSearchText);
      this.Controls.Add(this.comboBox1);
      this.Name = "Form1";
      this.Text = "Form1";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.TextBox tbSearchText;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Label labelMessage;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.RichTextBox richTextBox1;
  }
}

とりあえずこれで一応の動作はしますが、XMLの扱いがずさんなので、これから学んでいく必要があります。
ともあれ、コーディング中感じたのは、Visual Studioのコード補正と宣言元へのジャンプ機能の強力さ。統合開発環境も使いこなしていきたいです。

Home > C# Archive

バックナンバー
最近のコメント
最近のトラックバック
メタ情報

Return to page top