バーコードストアアプリ(WM6)完成!
前回までの記事
さてさて、とりあえず完成版です。
一応目的の機能は全て入っています。
CompactFrameworkなだけに少々妥協した点がありますが、とりあえず実用レベルの機能は入っています。
機能一覧
・読み取ったバーコードをテキストボックスに表示
・数量をタッチボタンから入力(キーボード入力は非サポート)
・データ保存機能(保存後バーコードと数量クリア、ファイル名自動(日付), MyDocumensに保存)
・1文字修正機能(Backspace)
・テキストクリア機能(Clear)
・オールクリア機能(AC)
・クローズ機能(ボタンで用意)
・保存ファイル命名規則→'BC'+年月日+'.csv' (例 BC09-08-24.csv)
・PCとのデータ交換はActiveSyncで手動で保存ファイルを引っ張りだします(とりあえず)
まだ細かいテストは終わってませんので、ご注意ください。
今後実際に使用しながら少しずつ改善していきたいと思います。
とりあえず、今日の状態↓
---------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;
namespace BarcodeHelper
{
class BarcodeHelperForm : System.Windows.Forms.Form
{
enum TextBoxes
{
Barcode = 0,
Qty
};
TextBoxes fcsTextbox = TextBoxes.Barcode;
String strBarcode = "";
String strQty = "0";
Label lblBarcode;
Label lblQuantity;
TextBox edtBarcode;
TextBox edtQuantity;
List btns = new List();
public BarcodeHelperForm()
{
this.Text="Barcode helper";
/* ======ボタン配列======
* [ 7 ] [ 8 ] [ 9 ] [<->]
* [ 4 ] [ 5 ] [ 6 ] [Enter]
* [ 1 ] [ 2 ] [ 3 ] [Close]
* [ 0 ] [ BS] [Clr] [ AC]
*/
btns.Add (new stBtnHandler(new Button(), "0" , btnQty_0_Click));
btns.Add (new stBtnHandler(new Button(), "BS" , btnBack_Click ));
btns.Add (new stBtnHandler(new Button(), "Clr" , btnClr_Click));
btns.Add (new stBtnHandler(new Button(), "AC" , btnAC_Click));
btns.Add (new stBtnHandler(new Button(), "1" , btnQty_1_Click));
btns.Add (new stBtnHandler(new Button(), "2" , btnQty_2_Click));
btns.Add (new stBtnHandler(new Button(), "3" , btnQty_3_Click));
btns.Add (new stBtnHandler(new Button(), "Close", btnClose_Click));
btns.Add (new stBtnHandler(new Button(), "4" , btnQty_4_Click));
btns.Add (new stBtnHandler(new Button(), "5" , btnQty_5_Click));
btns.Add (new stBtnHandler(new Button(), "6" , btnQty_6_Click));
btns.Add (new stBtnHandler(new Button(), "Enter", btnEnter_Click));
btns.Add (new stBtnHandler(new Button(), "7" , btnQty_7_Click));
btns.Add (new stBtnHandler(new Button(), "8" , btnQty_8_Click));
btns.Add (new stBtnHandler(new Button(), "9" , btnQty_9_Click));
btns.Add (new stBtnHandler(new Button(), "<->" , btnSwitch_Click));
// ラベル
lblBarcode = new Label();
lblBarcode.Text = "Barcode:";
lblBarcode.Location = new Point(13, 10);
lblBarcode.Size = new Size(155,19);
lblQuantity = new Label();
lblQuantity.Text = "Qty:";
lblQuantity.Location = new Point(170, 10);
lblQuantity.Size = new Size(50,19);
// エディットボックス
edtBarcode = new TextBox();
edtBarcode.Text = strBarcode;
edtBarcode.Location = new Point(13,30);
edtBarcode.Size = new Size(155,19);
edtBarcode.GotFocus += new EventHandler(edtBarcode_GotFocus);
edtQuantity = new TextBox();
edtQuantity.Location = new Point(173,30);
edtQuantity.Size = new System.Drawing.Size(50, 19);
edtQuantity.Text = strQty;
edtQuantity.GotFocus += new EventHandler(edtQuantity_GotFocus);
edtQuantity.AcceptsTab = true;
edtQuantity.AcceptsReturn = true;
// ボタン設定
int i = 0;
foreach(stBtnHandler btn in btns)
{
btn.Btn.Click += new EventHandler(btn.Click);
btn.Btn.Text = btn.Label;
btn.Btn.Location = new Point( (i % 4) * 55 + 13, 230 - (i / 4)*55 );
btn.Btn.Size = new Size(50,50);
i++;
}
SuspendLayout();
this.Controls.Add(lblBarcode);
this.Controls.Add(lblQuantity);
this.Controls.Add(edtBarcode);
this.Controls.Add(edtQuantity);
// ボタン登録
i = 0;
foreach(stBtnHandler btn in btns)
{
this.Controls.Add(btn.Btn);
}
ResumeLayout(false);
ReFocus();
}
// フォーカス移動(取得)イベント
void edtBarcode_GotFocus(object sender, EventArgs e)
{
fcsTextbox = TextBoxes.Barcode;
}
void edtQuantity_GotFocus(object sender, EventArgs e)
{
fcsTextbox = TextBoxes.Qty;
}
//クリックイベント
void btnClose_Click(object sender, EventArgs args)
{
this.Close();
}
void btnClr_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text = null;
} else {
edtQuantity.Text = "0";
}
ReFocus();
}
void btnAC_Click(object sender, EventArgs args)
{
edtBarcode.Text = null;
edtQuantity.Text = "0";
ReFocus();
}
string storeFile = null;
void btnEnter_Click(object sender, EventArgs args)
{
//バリデート処理 edtBarcode.Text
strBarcode = edtBarcode.Text;
//バリデート処理 edtQuantity.Text
string str = edtQuantity.Text;
int num = Convert.ToInt32(str);
strQty = num.ToString();
strQty = edtQuantity.Text;
// 保存処理
if( storeFile == null)
{
storeFile = "BC" + DateTime.Now.ToShortDateString()
.Replace(@"/", @"-").Replace("\\",@"-") + ".csv";
}
StreamWriter swStoreFile = new StreamWriter(
System.IO.Path.Combine("My Documents", storeFile),
true);
swStoreFile.WriteLine(strBarcode + "," + strQty);
swStoreFile.Close();
fcsTextbox = TextBoxes.Barcode;
strBarcode = "";
strQty = "0";
edtBarcode.Text = strBarcode;
edtQuantity.Text = strQty;
ReFocus();
}
void btnSwitch_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
fcsTextbox = TextBoxes.Qty;
} else {
fcsTextbox = TextBoxes.Barcode;
}
ReFocus();
}
void btnBack_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
if(edtBarcode.Text.Length > 0)
{
edtBarcode.Text = edtBarcode.Text.Remove(edtBarcode.Text.Length - 1, 1);
}
} else {
if(edtQuantity.Text.Length > 0)
{
edtQuantity.Text = edtQuantity.Text.Remove(edtQuantity.Text.Length - 1, 1);
}
if(edtQuantity.Text.Length == 0)
{
edtQuantity.Text = "0";
}
}
ReFocus();
}
void btnQty_0_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "0");
}
void btnQty_1_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "1");
}
void btnQty_2_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "2");
}
void btnQty_3_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "3");
}
void btnQty_4_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "4");
}
void btnQty_5_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "5");
}
void btnQty_6_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "6");
}
void btnQty_7_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "7");
}
void btnQty_8_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "8");
}
void btnQty_9_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "9");
}
private void ReFocus()
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Focus();
} else {
edtQuantity.Focus();
}
}
void btnQty_Click(object sender, EventArgs args, string strNum)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += strNum;
} else {
string str = edtQuantity.Text + strNum;
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
}
ReFocus();
}
[MTAThread]
static void Main()
{
BarcodeHelperForm mainForm = new BarcodeHelperForm();
Application.Run( mainForm );
}
}
public delegate void btn_Click(object sender, EventArgs args);
public struct stBtnHandler
{
public Button Btn; // Buttonオブジェクト
public string Label; // Lable
public btn_Click Click; // クリックイベント
public stBtnHandler(Button btObj, string strLbl, btn_Click btnClick)
{
this.Btn = btObj;
this.Label = strLbl;
this.Click = btnClick;
}
}
}
---------------------------------
C#リファクタリング と メトリクス測定
前回の日記『BTバーコードリーダー + WM6 』にてコードを記載しましたがだらだらと書いていて、同じ記述が繰り返されています。
本来は完成後のテストをクリアしてからもう一度リファクタリングを行い再度テストを行うというのがコードを綺麗に保つには良いのですが、今回はこの段階でリファクタリングを行います。
理由は簡単で完成までどんな変更が起こるかわからないからです。
つまりあまりファクタリングしすぎてしまうと後の設計変更で首が回らなくなるということです。
さて、言語はC#ですのでC#流のリファクタリング術が必要となります。
C言語のポインタや関数ポインタとパラメータの配列は割と全体が綺麗になるので好きなのですがC#の場合はAddメソッドでリストに追加していく方法が一般的です。
前回のコードは行数で393行でした。このうちリファクタリング対象の行数を数えてみると314行でした
さて今回書き直したコードは、全体で257行で、対象行数は160行とほぼ半分という結果になりました^^
さてさて、リファクタリング時にはその前とその後でメトリクスを比較しておくとその効果が定量的に把握できます。
今回のリファクタリングはメトリクス的にはどう変化したのでしょうか?
SouceMonitorというメトリクス測定ツールの測定結果を見てみましょう
http://www.campwoodsw.com/sourcemonitor.html
項目 変更前 変更後
-------------------------------
行数 393 257
ステートメント 289 144
%コメント 1.8 4.7
クラス数 1 2
平均コール数 8.67 4.57 ※1Methodあたり
平均ステートメント 13.39 4.86 ※1Methodあたりの
最大複雑度 6 6
最大深度 4 4
平均深度 2.11 1.83
平均複雑度 1.71 2.94
-------------------------------
ちゃんとメトリクス的にも改善されたことが分かりますね!
まだまだ改善の余地はありますが、とりあえずここまでとしています。
↓ソースコード
-------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace BarcodeHelper
{
class BarcodeHelperForm : System.Windows.Forms.Form
{
enum TextBoxes
{
Barcode = 0,
Qty
};
TextBoxes fcsTextbox = TextBoxes.Barcode;
String strBarcode = "";
String strQty = "0";
Label lblBarcode;
Label lblQuantity;
TextBox edtBarcode;
TextBox edtQuantity;
List btns = new List();
public BarcodeHelperForm()
{
this.Text="Barcode helper";
/* ======ボタン配列======
* [ 7 ] [ 8 ] [ 9 ] [<->]
* [ 4 ] [ 5 ] [ 6 ] [Enter]
* [ 1 ] [ 2 ] [ 3 ] [Close]
* [ 0 ] [ BS] [Clr] [ AC]
*/
btns.Add (new stBtnHandler(new Button(), "0" , btnQty_0_Click));
btns.Add (new stBtnHandler(new Button(), "BS" , btnBack_Click ));
btns.Add (new stBtnHandler(new Button(), "Clr" , btnClr_Click));
btns.Add (new stBtnHandler(new Button(), "AC" , btnAC_Click));
btns.Add (new stBtnHandler(new Button(), "1" , btnQty_1_Click));
btns.Add (new stBtnHandler(new Button(), "2" , btnQty_2_Click));
btns.Add (new stBtnHandler(new Button(), "3" , btnQty_3_Click));
btns.Add (new stBtnHandler(new Button(), "Close", btnClose_Click));
btns.Add (new stBtnHandler(new Button(), "4" , btnQty_4_Click));
btns.Add (new stBtnHandler(new Button(), "5" , btnQty_5_Click));
btns.Add (new stBtnHandler(new Button(), "6" , btnQty_6_Click));
btns.Add (new stBtnHandler(new Button(), "Enter", btnEnter_Click));
btns.Add (new stBtnHandler(new Button(), "7" , btnQty_7_Click));
btns.Add (new stBtnHandler(new Button(), "8" , btnQty_8_Click));
btns.Add (new stBtnHandler(new Button(), "9" , btnQty_9_Click));
btns.Add (new stBtnHandler(new Button(), "<->" , btnSwitch_Click));
// ラベル
lblBarcode = new Label();
lblBarcode.Text = "Barcode:";
lblBarcode.Location = new Point(13, 10);
lblBarcode.Size = new Size(155,19);
lblQuantity = new Label();
lblQuantity.Text = "Qty:";
lblQuantity.Location = new Point(170, 10);
lblQuantity.Size = new Size(50,19);
// エディットボックス
edtBarcode = new TextBox();
edtBarcode.Text = strBarcode;
edtBarcode.Location = new Point(13,30);
edtBarcode.Size = new Size(155,19);
edtQuantity = new TextBox();
edtQuantity.Location = new Point(173,30);
edtQuantity.Size = new System.Drawing.Size(50, 19);
edtQuantity.Text = strQty;
// ボタン設定
int i = 0;
foreach(stBtnHandler btn in btns)
{
btn.Btn.Click += new EventHandler(btn.Click);
btn.Btn.Text = btn.Label;
btn.Btn.Location = new Point( (i % 4) * 55 + 13, 230 - (i / 4)*55 );
btn.Btn.Size = new Size(50,50);
i++;
}
SuspendLayout();
this.Controls.Add(lblBarcode);
this.Controls.Add(lblQuantity);
this.Controls.Add(edtBarcode);
this.Controls.Add(edtQuantity);
// ボタン登録
foreach(stBtnHandler btn in btns)
{
this.Controls.Add(btn.Btn);
}
ResumeLayout(false);
ReFocus();
}
//クリックイベント
void btnClose_Click(object sender, EventArgs args)
{
this.Close();
}
void btnClr_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text = null;
} else {
edtQuantity.Text = "0";
}
ReFocus();
}
void btnAC_Click(object sender, EventArgs args)
{
edtBarcode.Text = null;
edtQuantity.Text = "0";
ReFocus();
}
void btnEnter_Click(object sender, EventArgs args)
{
//バリデート処理 edtBarcode.Text, edtQuantity.Text
strBarcode = edtBarcode.Text;
strQty = edtQuantity.Text;
ReFocus();
}
void btnSwitch_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
fcsTextbox = TextBoxes.Qty;
} else {
fcsTextbox = TextBoxes.Barcode;
}
ReFocus();
}
void btnBack_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
if(edtBarcode.Text.Length > 0)
{
edtBarcode.Text = edtBarcode.Text.Remove(edtBarcode.Text.Length - 1, 1);
}
} else {
if(edtQuantity.Text.Length > 0)
{
edtQuantity.Text = edtQuantity.Text.Remove(edtQuantity.Text.Length - 1, 1);
}
if(edtQuantity.Text.Length == 0)
{
edtQuantity.Text = "0";
}
}
ReFocus();
}
void btnQty_0_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "0");
}
void btnQty_1_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "1");
}
void btnQty_2_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "2");
}
void btnQty_3_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "3");
}
void btnQty_4_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "4");
}
void btnQty_5_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "5");
}
void btnQty_6_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "6");
}
void btnQty_7_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "7");
}
void btnQty_8_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "8");
}
void btnQty_9_Click(object sender, EventArgs args)
{
btnQty_Click(sender, args, "9");
}
private void ReFocus()
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Focus();
} else {
edtQuantity.Focus();
}
}
void btnQty_Click(object sender, EventArgs args, string strNum)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += strNum;
} else {
string str = edtQuantity.Text + strNum;
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
}
ReFocus();
}
[MTAThread]
static void Main()
{
BarcodeHelperForm mainForm = new BarcodeHelperForm();
Application.Run( mainForm );
}
}
public delegate void btn_Click(object sender, EventArgs args);
public struct stBtnHandler
{
public Button Btn; // Buttonオブジェクト
public string Label; // Lable
public btn_Click Click; // クリックイベント
public stBtnHandler(Button btObj, string strLbl, btn_Click btnClick)
{
this.Btn = btObj;
this.Label = strLbl;
this.Click = btnClick;
}
}
}
-------------------------------------
つづく
BTバーコードリーダー + WM6
先日の日記『無償ツールで構築するWindows Mobile 開発環境 』ではタイトルどおり無償ツールでWMの開発環境の構築を行いました。この環境をベースにアプリケーションを開発します。
安価なブルートゥースバーコードリーダーで在庫管理を行っていますが、単にバーコードを読み取りホストへキー入力として送信するものを使っております。
部品のピッキング時の効率アップのためにバーコードのホストをWindows Mobile(以下WM)として一旦WMへバーコードをおとし、後で在庫管理システムへデータを転送する仕組みに変えようと思っています。
あわせてWMでは数量の入力を行えるような作りこみをします。
在庫管理システムへの転送の方法は後日検討するとして、とりあえずWMへファイル保存する方式で検討します。
使用するバーコードリーダーは下記のものです。
http://item.rakuten.co.jp/mobileplaza/6223/
ワイヤレスの割りに7千円台と結構お徳ではないかと思います。
まだ、完成度は低いですが保存以外の機能と細かな部分を気にしなければおおよそ完成です。
↓は起動時の画面です。
繰り返しですがバーコードの読み取りはキー入力として入ってきますそのため、バーコード読み取りのときはテキストボックスのフォーカスをBarcode側に人の操作で移動しておく必要があります。
下の数字は数量入力用に配置しています(Barcodeの数値入力も可能)。
バーコードのクリアは、フォーカスをBarcodeへ移したあとCLRボタンかACボタンでクリアします。
BSはバックスペースキーです。
『<->』ボタンはBarcodeと数量のフォーカスを切り替えます。
どちらにフォーカスがあたっているかはテキストボックスのカーソル表示で確認できます。
まだまだ改良の余地+必要機能がありますが、途中経過としてアップしておきます。
下記はソースコード
----------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyNamespace
{
class HelloWorldForm : System.Windows.Forms.Form
{
enum TextBoxes {Barcode = 0, Qty};
TextBoxes fcsTextbox = TextBoxes.Barcode;
String strBarcode = ""; //"please read the barcoe!";
String strQty = "0";
Label lblBarcode;
Label lblQuantity;
Button btnQty_0;
Button btnQty_1;
Button btnQty_2;
Button btnQty_3;
Button btnQty_4;
Button btnQty_5;
Button btnQty_6;
Button btnQty_7;
Button btnQty_8;
Button btnQty_9;
Button btnClose;
Button btnBack;
Button btnClr;
Button btnAC;
Button btnSwitch;
Button btnEnter;
TextBox edtBarcode;
TextBox edtQuantity;
public HelloWorldForm()
{
this.Text="Barcode helper";
btnClose = new Button();
btnBack = new Button();
btnClr = new Button();
btnAC = new Button();
btnSwitch = new Button();
btnEnter = new Button();
btnQty_0 = new Button();
btnQty_1 = new Button();
btnQty_2 = new Button();
btnQty_3 = new Button();
btnQty_4 = new Button();
btnQty_5 = new Button();
btnQty_6 = new Button();
btnQty_7 = new Button();
btnQty_8 = new Button();
btnQty_9 = new Button();
lblBarcode = new Label();
lblQuantity = new Label();
edtBarcode = new TextBox();
edtQuantity = new TextBox();
btnClose.Click += new EventHandler(btnClose_Click);
btnClose.Text="Close";
btnClose.Location = new Point(178,175);
btnClose.Size = new Size(50,50);
// Back space
btnBack.Click += new EventHandler(btnBack_Click);
btnBack.Text="BS";
btnBack.Location = new Point(68,230);
btnBack.Size = new Size(50,50);
// Clr
btnClr.Click += new EventHandler(btnClr_Click);
btnClr.Text="CLR";
btnClr.Location = new Point(123,230);
btnClr.Size = new Size(50,50);
// All Clear
btnAC.Click += new EventHandler(btnAC_Click);
btnAC.Text="AC";
btnAC.Location = new Point(178,230);
btnAC.Size = new Size(50,50);
// Switch
btnSwitch.Click += new EventHandler(btnSwitch_Click);
btnSwitch.Text="<->";
btnSwitch.Location = new Point(178,65);
btnSwitch.Size = new Size(50,50);
// Enter
btnEnter.Click += new EventHandler(btnEnter_Click);
btnEnter.Text="Ent";
btnEnter.Location = new Point(178,120);
btnEnter.Size = new Size(50,50);
btnQty_0.Click += new EventHandler(btnQty_0_Click);
btnQty_0.Text="0";
btnQty_0.Location = new Point(13,230);
btnQty_0.Size = new Size(50,50);
btnQty_1.Click += new EventHandler(btnQty_1_Click);
btnQty_1.Text="1";
btnQty_1.Location = new Point(13,175);
btnQty_1.Size = new Size(50,50);
btnQty_2.Click += new EventHandler(btnQty_2_Click);
btnQty_2.Text="2";
btnQty_2.Location = new Point(68,175);
btnQty_2.Size = new Size(50,50);
btnQty_3.Click += new EventHandler(btnQty_3_Click);
btnQty_3.Text="3";
btnQty_3.Location = new Point(123,175);
btnQty_3.Size = new Size(50,50);
btnQty_4.Click += new EventHandler(btnQty_4_Click);
btnQty_4.Text="4";
btnQty_4.Location = new Point(13,120);
btnQty_4.Size = new Size(50,50);
btnQty_5.Click += new EventHandler(btnQty_5_Click);
btnQty_5.Text="5";
btnQty_5.Location = new Point(68,120);
btnQty_5.Size = new Size(50,50);
btnQty_6.Click += new EventHandler(btnQty_6_Click);
btnQty_6.Text="6";
btnQty_6.Location = new Point(123,120);
btnQty_6.Size = new Size(50,50);
btnQty_7.Click += new EventHandler(btnQty_7_Click);
btnQty_7.Text="7";
btnQty_7.Location = new Point(13,65);
btnQty_7.Size = new Size(50,50);
btnQty_8.Click += new EventHandler(btnQty_8_Click);
btnQty_8.Text="8";
btnQty_8.Location = new Point(68,65);
btnQty_8.Size = new Size(50,50);
btnQty_9.Click += new EventHandler(btnQty_9_Click);
btnQty_9.Text="9";
btnQty_9.Location = new Point(123,65);
btnQty_9.Size = new Size(50,50);
lblBarcode.Text = "Barcode:";
lblBarcode.Location = new Point(13, 10);
lblBarcode.Size = new Size(155,19);
lblQuantity.Text = "Qty:";
lblQuantity.Location = new Point(170, 10);
lblQuantity.Size = new Size(50,19);
edtBarcode.Text = strBarcode;
edtBarcode.Location = new Point(13,30);
edtBarcode.Size = new Size(155,19);
edtQuantity.Location = new Point(173,30);
edtQuantity.Size = new System.Drawing.Size(50, 19);
edtQuantity.Text = strQty;
SuspendLayout();
this.Controls.Add(lblBarcode);
this.Controls.Add(lblQuantity);
this.Controls.Add(btnClose);
this.Controls.Add(btnBack);
this.Controls.Add(btnClr);
this.Controls.Add(btnAC);
this.Controls.Add(btnSwitch);
this.Controls.Add(btnEnter);
this.Controls.Add(btnQty_0);
this.Controls.Add(btnQty_1);
this.Controls.Add(btnQty_2);
this.Controls.Add(btnQty_3);
this.Controls.Add(btnQty_4);
this.Controls.Add(btnQty_5);
this.Controls.Add(btnQty_6);
this.Controls.Add(btnQty_7);
this.Controls.Add(btnQty_8);
this.Controls.Add(btnQty_9);
this.Controls.Add(edtBarcode);
this.Controls.Add(edtQuantity);
ResumeLayout(false);
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Focus();
} else {
edtQuantity.Focus();
}
}
void btnClose_Click(object sender, EventArgs args)
{
this.Close();
}
void btnClr_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text = null;
edtBarcode.Focus();
} else {
edtQuantity.Text = "0";
edtQuantity.Focus();
}
}
void btnAC_Click(object sender, EventArgs args)
{
edtBarcode.Text = null;
edtQuantity.Text = "0";
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Focus();
} else {
edtQuantity.Focus();
}
}
void btnEnter_Click(object sender, EventArgs args)
{
//バリデート処理 edtBarcode.Text, edtQuantity.Text
strBarcode = edtBarcode.Text;
strQty = edtQuantity.Text;
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Focus();
} else {
edtQuantity.Focus();
}
}
void btnSwitch_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
fcsTextbox = TextBoxes.Qty;
edtQuantity.Focus();
} else {
fcsTextbox = TextBoxes.Barcode;
edtBarcode.Focus();
}
}
void btnBack_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
if(edtBarcode.Text.Length > 0)
{
edtBarcode.Text = edtBarcode.Text.Remove(edtBarcode.Text.Length - 1, 1);
}
edtBarcode.Focus();
} else {
if(edtQuantity.Text.Length > 0)
{
edtQuantity.Text = edtQuantity.Text.Remove(edtQuantity.Text.Length - 1, 1);
}
if(edtQuantity.Text.Length == 0)
{
edtQuantity.Text = "0";
}
edtQuantity.ScrollToCaret();
edtQuantity.Focus();
}
}
void btnQty_0_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "0";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "0";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_1_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "1";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "1";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_2_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "2";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "2";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_3_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "3";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "3";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_4_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "4";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "4";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_5_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "5";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "5";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_6_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "6";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "6";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_7_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "7";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "7";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_8_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "8";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "8";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
void btnQty_9_Click(object sender, EventArgs args)
{
if(fcsTextbox == TextBoxes.Barcode)
{
edtBarcode.Text += "9";
edtBarcode.Focus();
} else {
string str = edtQuantity.Text + "9";
int num = Convert.ToInt32(str);
edtQuantity.Text = num.ToString();
edtQuantity.Focus();
}
}
[MTAThread]
static void Main()
{
HelloWorldForm mainForm = new HelloWorldForm();
Application.Run( mainForm );
}
}
}
----------------------------------------------
つづく
