(WinForm,C#扫描枪
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Xml;using System.Configuration;namespace Scan_Pro.View{ /// <summary> /// 桶的扫描操作类 /// </summary> public partial class frmScanOpterate : Form { public frmScanOpterate() { InitializeComponent();}#region 窗体属性加载private void frmScanOpterate_Load(object sender, EventArgs e){if (!System.IO.File.Exists(Scan_Pro.Opteration.ScanOpterate.mStrFileName)) {frmInit frmIn = new Scan_Pro.frmInit();frmIn.Text = "初始化桶";frmIn.ShowDialog();}//默认设置桶(最大容量和最大数目)Scan_Pro.Opteration.ScanOpterate.LoadSetDefaultBoxCoutMaxAndBoxCapactity();//刷新桶中的数据//Scan_Pro.Opteration.ScanOpterate.LoadNum(listView);//利用ListView实现//Scan_Pro.Opteration.ScanOpterate.LoadNumber(dataGridView);//方法1Scan_Pro.Opteration.ScanOpterate.InitBoxToDataGridView(dataGridView);Scan_Pro.Opteration.ScanOpterate.LoadAllBarCode(dataGridView);//方法2(最优化的方法)}#endregion#region 取消private void btnCancel_Click(object sender, EventArgs e) {Application.Exit();}#endregion#region 确定private void btnStartScan_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor;if (txtPageNum.Text.Trim() == ""){Scan_Pro.View.ZMessageBox.Show("", "条码不能为空!", 2);Cursor = Cursors.Default; return;}int BoxNum;string pStr = GetstrTrim(txtPageNum.Text.Trim());int Stat = Scan_Pro.Opteration.ScanOpterate.ScanStat(pStr, out BoxNum);if(Stat==1)//扫描到条码{DialogResult drIs=ZMessageBox.Show(BoxNum.ToString(),1);if (drIs.ToString() == "OK"){Scan_Pro.Opteration.ScanOpterate.setScanNumOk(pStr);txtPageNum.Clear();}}else if(Stat==3)//没有扫描到,桶中有空位{DialogResult dr1 =ZMessageBox.Show(BoxNum.ToString(),2);if (dr1.ToString() == "OK")//追加该号码到容积中{Scan_Pro.Opteration.ScanOpterate.AddPageNum(pStr);txtPageNum.Clear();}}else if (Stat == 2)//没有扫描到,桶中没有空位{DialogResult dr = Scan_Pro.View.ZMessageBox.Show("", "桶已经装满(按F11键扩容)", 2);if (dr.ToString() == "OK"){frmInit frmIn = new frmInit();frmIn.ShowDialog();Scan_Pro.Opteration.ScanOpterate.InitBoxToDataGridView(dataGridView);Scan_Pro.Opteration.ScanOpterate.AddPageNum(pStr);txtPageNum.Clear();}}else{DialogResult dr = Scan_Pro.View.ZMessageBox.Show("", "请追加桶!", 2);if (dr.ToString() == "OK"){frmInit frmIn = new frmInit();frmIn.Text = "追加桶";frmIn.ShowDialog();Scan_Pro.Opteration.ScanOpterate.InitBoxToDataGridView(dataGridView);Scan_Pro.Opteration.ScanOpterate.AddPageNum(pStr);txtPageNum.Clear();}}txtPageNum.SelectAll(); Cursor = Cursors.Default;//Scan_Pro.Opteration.ScanOpterate.LoadNumber(dataGridView);//方法1Scan_Pro.Opteration.ScanOpterate.LoadAllBarCode(dataGridView);//方法2}#endregion#region 快捷键和输入字符限制实现private void frmScanOpterate_KeyDown(object sender, KeyEventArgs e){if (e.KeyCode == Keys.Escape){DialogResult dr = Scan_Pro.View.ZMessageBox.Show("", "确认要退出码?", 2); ;if(dr.ToString()=="OK")Application.Exit();}else if (e.KeyCode == Keys.Enter)btnStartScan_Click(sender, new EventArgs());else if (e.KeyCode == Keys.F11){Scan_Pro.frmInit frmInit = new Scan_Pro.frmInit();frmInit.Text = "扩容";frmInit.ShowDialog();Scan_Pro.Opteration.ScanOpterate.InitBoxToDataGridView(dataGridView);Scan_Pro.Opteration.ScanOpterate.LoadAllBarCode(dataGridView);}else if(e.Control)//删除选中的条码{if(e.KeyCode==Keys.Delete){Scan_Pro.Opteration.ScanOpterate.DeleteNumSelected(dataGridView);Scan_Pro.Opteration.ScanOpterate.InitBoxToDataGridView(dataGridView);Scan_Pro.Opteration.ScanOpterate.LoadAllBarCode(dataGridView);//方法2(最优化的方法)}}else if (e.KeyCode == Keys.F2)//初始化桶{if (System.IO.File.Exists(Scan_Pro.Opteration.ScanOpterate.mStrFileName)){DialogResult dr = Scan_Pro.View.ZMessageBox.Show("清空", "所有条码吗?", 4);if (dr.ToString() == "OK"){if (System.IO.File.Exists(Scan_Pro.Opteration.ScanOpterate.mStrFileName)){Scan_Pro.Opteration.ScanOpterate.DeleteAllNodeNum();Scan_Pro.Opteration.ScanOpterate.InitBoxToDataGridView(dataGridView);Scan_Pro.Opteration.ScanOpterate.LoadAllBarCode(dataGridView);}}}}else if (e.KeyCode == Keys.F1)//帮助{Scan_Pro.View.frmHelp frmSh = new frmHelp();frmSh.ShowDialog();}else if (e.KeyCode == Keys.F5){//Scan_Pro.Opteration.ScanOpterate.LoadAllBarCode(dataGridView);}else if (e.KeyCode == Keys.F7)//设置桶和容积的上限{frmInit frmIn = new frmInit("设置桶和容积的上限");frmIn.ShowDialog();}if (e.KeyCode == Keys.Back){IsBackspace = true;}else {IsBackspace = false;}}private bool nonNumberEntered = false;private void txtPageNum_KeyDown(object sender, KeyEventArgs e){CheckInputChar(sender,e);}private void txtPageNum_KeyPress(object sender, KeyPressEventArgs e){if(nonNumberEntered){e.Handled = true;}}//只接受数字和字符(a---z)或者(A--Z)private bool CheckInputChar(object sender, KeyEventArgs e){nonNumberEntered = false;// Determine whether the keystroke is a number from the top of the keyboard.if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9){// Determine whether the keystroke is a number from the keypad.if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9){// Determine whether the keystroke is a backspace.if (e.KeyCode != Keys.Back){// A non-numerical keystroke was pressed.// Set the flag to true and evaluate in KeyPress event.nonNumberEntered = true;}}}if (e.KeyCode >=Keys.A && e.KeyCode <= Keys.Z){nonNumberEntered = false;}return nonNumberEntered;}#endregion#region 格式化输入字符串//字符格式化(每5个字符加一个空格)private bool IsSuss = false;private bool IsBackspace = false;private void txtPageNum_TextChanged(object sender, EventArgs e){if (txtPageNum.Text!= ""){string pStrInput = txtPageNum.Text;char []chBefore = pStrInput.ToCharArray();string pStrNew = "";foreach(char ch in chBefore){if(ch!=' ')pStrNew =ch.ToString();}char[] chNow = pStrNew.ToCharArray();string pStrNow = "";for (int i = 0; i < chNow.Length; i ){if (i % 5 == 0 && i >=5){pStrNow = " ";}pStrNow = chNow[i].ToString();if (i == chNow.Length - 1) {IsSuss = true;}}txtPageNum.Select(pStrNow.Length, pStrNow.Length);if (IsSuss && !IsBackspace) {IsSuss = false;txtPageNum.Text = pStrNow;return;}}}#endregionprivate string GetstrTrim( string pStrInput) {string pStrNew = "";foreach (char ch in pStrInput.ToCharArray()){if (ch != ' ')pStrNew = ch.ToString();}return pStrNew;}private void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e){if (e.ColumnIndex==-1 || e.RowIndex==-1) return;if (dataGridView.Rows.Count != 0){if (e.ColumnIndex > 1){if (e.Button == MouseButtons.Left){dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true;}}else {dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = false;}}}}}
评论