public Main()
{
InitializeComponent();
this.txtbarcode.Focus();
}
//扫描txt文本文件查询操作
private void BarCodeScan()
{
string code = this.txtbarcode.Text.Trim();
string fileName = "barcodeinfo.txt";
StreamReader sr = new StreamReader(fileName, Encoding.Default);//以gb2312字符编码格式读取文本。
string str;
int count=0;
while ((str = sr.ReadLine()) != null)//读取每一行
{
if (str.IndexOf(code) == 0)
{
this.listBoxCode.Items.Add(str);
this.listBoxCode.SelectedIndex = this.listBoxCode.Items.Count - 1;
count = count + 1;
}
}
if (count == 0)
{
this.listBoxCode.Items.Add(code + " 无条码信息");
this.listBoxCode.SelectedIndex = this.listBoxCode.Items.Count - 1;
}
sr.Close();
this.txtbarcode.Text = null;
}
//点击enter后的动作
private void txtbarcode_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == "/r")
{
BarCodeScan();
}
}
//删除按钮功能
private void BtnDelete_Click(object sender, EventArgs e)
{
if (this.listBoxCode.SelectedItem == null)
{
MessageBox.Show("请先选择需要删除的行!");
}
else
{
DialogResult MsgBoxResult;//设置对话框的返回值
MsgBoxResult = MessageBox.Show("你确定要删除此条码?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
if (MsgBoxResult == DialogResult.Yes)//如果对话框的返回值是YES(按"Y"按钮)
{
this.listBoxCode.Items.Remove(listBoxCode.SelectedItem);
this.txtbarcode.Focus();
}
if (MsgBoxResult == DialogResult.No)//如果对话框的返回值是NO(按"N"按钮)
{
this.txtbarcode.Focus();
}
}
}
// 提取字符串中为数字的字符串
public string getNum(String str)
{
string ss="";
for (int i = 0; i < str.Length; i++)
{
if (Char.IsNumber(str, i) == true)
{
ss += str.Substring(i, 1);
}
else
{
if (str.Substring(i, 1) == ",")
{
ss += str.Substring(i, 1);
}
}
}
return ss;
}
private void BtnOut_Click(object sender, EventArgs e)
{
if (this.listBoxCode.Items.Count == 0)
{
MessageBox.Show("没有数据可导出!");
}
else
{
using (StreamWriter sw = new StreamWriter("barcode.txt",true))
{
for (int i = 0; i < listBoxCode.Items.Count; i++)
{
string str = listBoxCode.Items[i].ToString();
sw.WriteLine(getNum(str));
}
MessageBox.Show("数据导出成功!");
}
}
}
private void BtnReset_Click(object sender, EventArgs e)
{
if (this.listBoxCode.Items.Count == 0)
{
MessageBox.Show("条码列表无数据!");
}
else
{
DialogResult MsgBoxResult;//设置对话框的返回值
MsgBoxResult = MessageBox.Show("你确定要清空条码列表?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);
if (MsgBoxResult == DialogResult.Yes)//如果对话框的返回值是YES(按"Y"按钮)
{
this.listBoxCode.Items.Clear();
this.txtbarcode.Focus();
}
if (MsgBoxResult == DialogResult.No)//如果对话框的返回值是NO(按"N"按钮)
{
this.txtbarcode.Focus();
}
}
}