バーコードストアアプリ(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;
}
}
}
---------------------------------