using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.IO.Ports;
using System.Data.Odbc;


namespace DL580_9130
{
    public partial class main : Form
    {
        string[] str = new string[8];

    //    byte[] Data=new byte[5];
        public main()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "") { return; }

            byte linktype = 1;      //为1代表串口，为2代表网络接口,为3代表USB接口。
            string com_port = null;
            if (comboBox1.SelectedIndex == 0)       //TCP/IP
            {
                linktype = 2;
                com_port = comboBox2.Text;
            }
            else if (comboBox1.SelectedIndex == 1)  //USB
            {
                linktype = 3;
            }
            else                    //com
            {
                linktype = 1;
                com_port = comboBox1.Text;
            }
            this.Cursor = Cursors.WaitCursor; 
            short Result =readerDLL.OpenReader(ref readerDLL.hComm, linktype,com_port);
            if (Result == 0)            //0代表执行正确，其它为错误
            {
                timer3.Enabled = true;
 
                ShowCommandtxt(Properties.Settings.Default.Setting49 + Properties.Settings.Default.Setting28);
                if (comboBox1.SelectedIndex == 2)  //com
                {
                    Result=readerDLL.SetBaudRate(readerDLL.hComm, comboBox2.SelectedIndex);     //如为串口通信则设置波特率
                    if (Result == 0)
                    {
                        ShowCommandtxt( Properties.Settings.Default.Setting26);
                    }
                    else
                    {
                        ShowCommandtxt( Properties.Settings.Default.Setting27);
                        this.Cursor = Cursors.Default;
                        return;
                    }
                }
                if (!Getversions())
                {
                    ShowCommandtxt(Properties.Settings.Default.Setting50);
                    this.Cursor = Cursors.Default;
                    return ;
                }
                button1.Enabled = false;
                Setstatus(!button1.Enabled);
            }
            else
            {
                ShowCommandtxt( Properties.Settings.Default.Setting25);
            }
            this.Cursor = Cursors.Default;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            short Result=readerDLL.CloseReader(readerDLL.hComm);
            if (Result == 0)
            {
                button1.Enabled = true;
                Setstatus(!button1.Enabled);
            }
            else
            {
                ShowCommandtxt( Properties.Settings.Default.Setting25);
            }
            stopdo();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnCount = 8;
            dataGridView1.Columns[0].HeaderText = Properties.Settings.Default.Setting;
            dataGridView1.Columns[1].HeaderText  = Properties.Settings.Default.Setting21;
            dataGridView1.Columns[2].HeaderText = Properties.Settings.Default.Setting1;
            dataGridView1.Columns[3].HeaderText = Properties.Settings.Default.Setting2;
            dataGridView1.Columns[4].HeaderText = Properties.Settings.Default.Setting3;
            dataGridView1.Columns[5].HeaderText = Properties.Settings.Default.Setting4;
            dataGridView1.Columns[6].HeaderText = Properties.Settings.Default.Setting5;
            dataGridView1.Columns[7].HeaderText = Properties.Settings.Default.Setting6;
            for (int tmp = 0; tmp < dataGridView1.Columns.Count; tmp++)
            {
                dataGridView1.Columns[tmp].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            comboBox1.Items.Add ("TCP/IP");
            comboBox1.Items.Add("USB");
            string[] comname = System.IO.Ports.SerialPort.GetPortNames();
            for (int tmp = 0; tmp < comname.Length; tmp++)
            {
                comboBox1.Items.Add(comname[tmp]);
            }
            readtxt.Text = Properties.Settings.Default.Setting14;
            tabPage3.Text=  Properties.Settings.Default.Setting20;
            tabPage4.Text = Properties.Settings.Default.Setting23;
            button9.Text   = Properties.Settings.Default.Setting35+button4.Text;
            button10.Text = Properties.Settings.Default.Setting35 + button5.Text;
            button12.Text = Properties.Settings.Default.Setting68;
            database.Open();
            readini();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Enabled = true;
            comboBox2 .Items.Clear ();
            comboBox2.Text = ""; 
 
            if (comboBox1.SelectedIndex == 0)       //TCP/IP
            {
                label2.Text = Properties.Settings.Default.Setting11;
                comboBox2.DropDownStyle = ComboBoxStyle.Simple;
                comboBox2.Text = "192.168.0.178";
            }
            else if (comboBox1.SelectedIndex == 1)  //USB
            {
                comboBox2.Enabled = false;
            }
            else            ////串口   com
            {
                label2.Text = Properties.Settings.Default.Setting10;
                string[] bps = { "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400" };
                for (int tmp = 0; tmp < bps.Length; tmp++)
                {
                    comboBox2.Items.Add(bps[tmp].ToString() + "bps");
                }
                comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
                comboBox2.SelectedIndex = 2;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            short Result=readerDLL.ResetReader(readerDLL.hComm);
            if (Result == 0)
            {
                timer3.Enabled = true;
                ShowCommandtxt(button3.Text + Properties.Settings.Default.Setting28);
                button2_Click(sender, e);
            }
            else
            {
                ShowCommandtxt( Properties.Settings.Default.Setting25);
            }
            stopdo();
        }
        private bool Getversions()
        { 
            byte flag=0, major=0, minor=0;
            short Result = readerDLL.GetFirmwareVersion(readerDLL.hComm, ref flag, ref major, ref minor);
            if (Result == 0)
            {
                timer3.Enabled = true;
                vertxt.Text = tButton1.Text + "v" + major.ToString() + "." + minor.ToString(); 
            }
            return (Result == 0);
        }
        private void tButton1_Click(object sender, EventArgs e)
        {
            if (Getversions())
            {
                MessageBox.Show(vertxt.Text + "   ", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            byte TagType = 0;
            byte[] value = new byte[128];
            short Result = readerDLL.SingleTagIdentifyEX(readerDLL.hComm, ref TagType, value);
            if (Result == 0 )
            {
                ShowCommandtxt(button4.Text + Properties.Settings.Default.Setting28);
                if (TagType > 0)
                {
                    string CardId = "";
                    int Length = GetTagLength(TagType);
                    for (int tmp = 0; tmp < Length; tmp++)
                    {
                        CardId = CardId + value[tmp].ToString("X2");    //X  16进制 2:两个位不足前面补0
                    }
                    string str1 = Properties.Settings.Default.Setting22 + "1";
                    ShowToGrid(str1, CardId,"","","",true);
                    SaveToDadatabase(CardId);
                }
                else
                {
                    ShowToGrid(Properties.Settings.Default.Setting39, "", "", "","", true);
                } 
            }
            else
            {
                ShowCommandtxt( Properties.Settings.Default.Setting25);
            }
        }
        private void main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (button2.Enabled) { button2_Click(sender, e); }
            database.Close();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            if(MultiIDCheck(true))
            {
                ShowCommandtxt(button5.Text + Properties.Settings.Default.Setting28);
            }
            else
            {
                ShowCommandtxt( Properties.Settings.Default.Setting25);
            }
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void shownull_Click(object sender, EventArgs e)
        {
            dataGridView1.RowCount = 0; 
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (!scantxt()) { return; }
            this.Cursor = Cursors.WaitCursor; 
            byte[] tagid = new byte[4];
            string str = "00000000" + textBox1.Text.Trim ();
            tagid[3] = Convert.ToByte(str.Substring(str.Length - 2, 2), 16);
            tagid[2] = Convert.ToByte(str.Substring(str.Length - 4, 2), 16);
            tagid[1] = Convert.ToByte(str.Substring(str.Length - 6, 2), 16);
            tagid[0] = Convert.ToByte(str.Substring(str.Length - 8, 2), 16);

            int Add = int.Parse(textBox2.Text);
            byte count = byte.Parse(textBox3.Text);
            byte[] buf = new byte[int.Parse(textBox3.Text)];
            short Result=readerDLL.ReadTagUserWithID(readerDLL.hComm ,count,Add ,tagid,buf );
            if (Result == 0)
            {
                timer3.Enabled = true;
                textBox5.Text = "";
                for (int tmp = 0; tmp < buf.Length; tmp++)
                {
                    textBox5.Text = textBox5.Text + buf[tmp].ToString("X2")+" ";
                }
                ShowCommandtxt(button7.Text + Properties.Settings.Default.Setting28);
                MessageBox.Show(button7.Text + Properties.Settings.Default.Setting28);
            }
            else
            {
                ShowErr(Result);    
            }
            this.Cursor = Cursors.Default; 
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "0123456789ABCDEF\b".IndexOf(char.ToUpper(e.KeyChar)) < 0;  //  "\b"退格键
        }
        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "0123456789\b".IndexOf(char.ToUpper(e.KeyChar)) < 0;  
        }
        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "0123456\b".IndexOf(char.ToUpper(e.KeyChar)) < 0;
        }
        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "0123456789ABCDEF\b".IndexOf(char.ToUpper(e.KeyChar)) < 0;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            if (!scantxt()) { return; }
            this.Cursor = Cursors.WaitCursor;
            byte[] tagid = new byte[4];
            string str = "00000000" + textBox1.Text.Trim();
            tagid[3] = Convert.ToByte(str.Substring(str.Length - 2, 2), 16);
            tagid[2] = Convert.ToByte(str.Substring(str.Length - 4, 2), 16);
            tagid[1] = Convert.ToByte(str.Substring(str.Length - 6, 2), 16);
            tagid[0] = Convert.ToByte(str.Substring(str.Length - 8, 2), 16);
            int Add = int.Parse(textBox2.Text);
            byte count = byte.Parse(textBox3.Text);

            if (textBox4.Text.Trim() == "") { textBox4.Text = "00"; }
            if (textBox4.Text.Length % 2 != 0) { textBox4.Text = "0" + textBox4.Text; }
            byte[] buf = new byte[textBox4.Text.Length / 2];
            for (int tmp = 0; tmp < textBox4.Text.Length / 2; tmp++)
            {
                buf[tmp] =Convert.ToByte(textBox4.Text.Substring(tmp * 2, 2),16); 
            }
            short Result = readerDLL.WriteTagUserWithID(readerDLL.hComm, count, Add, tagid, buf,0);
            if (Result == 0)
            {
                timer3.Enabled = true;
                ShowCommandtxt(button6.Text + Properties.Settings.Default.Setting28);
                MessageBox.Show(button6.Text + Properties.Settings.Default.Setting28);
            }
            else
            {
                ShowErr(Result);
            }
            this.Cursor = Cursors.Default;

        }
        private void button8_Click(object sender, EventArgs e)
        {
            byte TagType = 0;
            byte[] value = new byte[128];
            short Result = readerDLL.SingleTagIdentifyEX(readerDLL.hComm, ref TagType, value);
            if (Result == 0)
            {
                timer3.Enabled = true;
                if (TagType > 0)
                {
                    string CardId = "";
                    int Length = GetTagLength(TagType);
                    for (int tmp = 0; tmp < Length; tmp++)
                    {
                        CardId = CardId + value[tmp].ToString("X2");    //X  16进制 2:两个位不足前面补0
                    }
                    textBox1.Text = CardId.Substring(CardId.Length - 8, 8);
                }
                else
                {
                    MessageBox.Show(Properties.Settings.Default.Setting39);
                }
            }
            else
            {
                ShowCommandtxt(Properties.Settings.Default.Setting25);
            }
        }

        //*****************************************************************************************
        private bool scantxt()
        {
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show(Properties.Settings.Default.Setting41 + label3.Text);
                textBox1.Focus(); 
                return false ;
            }
            if (textBox2.Text.Trim() == "")
            {
                MessageBox.Show(Properties.Settings.Default.Setting41 + label4.Text);
                textBox2.Focus(); 
                return false ;
            }
            if (textBox3.Text.Trim() == "")
            {
                MessageBox.Show(Properties.Settings.Default.Setting41 + label5.Text);
                textBox3.Focus(); 
                return false ;
            }
            return true;
        }
        private void Setstatus(bool val)
        {
            button2.Enabled = val;
            button3.Enabled = val;
            tButton1.Enabled = val;
            readtxt.Text = button2.Enabled ? Properties.Settings.Default.Setting13 : Properties.Settings.Default.Setting14;
            button4.Enabled = val;
            button5.Enabled = val;
            button6.Enabled = val;
            button7.Enabled = val;
            button8.Enabled = val;
            button9.Enabled = val;
            button10.Enabled = val;
            button12.Enabled = val; 
        }
        private void ShowErr(short Errnumber)
        {
            if (Errnumber < 0) { return; }
            string Errstr = null;
            switch (Errnumber)
            {
                case 1:
                    Errstr = Properties.Settings.Default.Setting25;
                    break;
                case 2:
                    Errstr = Properties.Settings.Default.Setting42;
                    break;
                case 3:
                    Errstr = Properties.Settings.Default.Setting43;
                    break;
                case 4:
                    Errstr = Properties.Settings.Default.Setting44;
                    break;
                case 5:
                    Errstr = Properties.Settings.Default.Setting45;
                    break;
                case 16:
                    Errstr = Properties.Settings.Default.Setting46;
                    break;
                case 245:
                    Errstr = Properties.Settings.Default.Setting47;
                    break;
            }
            ShowCommandtxt(Errstr);
            MessageBox.Show(Errstr);
        }
        private int GetTagLength(int TagType)
        {
            int Length = 0;
            if (TagType == 8)
            {
                Length = 8;
            }
            else if (TagType == 9)
            {
                Length = 4;
            }
            else if (TagType == 10)
            {
                Length = 6;
            }
            return Length;
        }
        private void ShowCommandtxt(string str1)
        {
            commandtxt.Items.Add(DateTime.Now.ToLongTimeString() + "   " + str1);
            commandtxt.SelectedIndex = commandtxt.Items.Count - 1;
        }
        private void ShowToGrid(string str1, string str2, string str3, string str4, string str5, bool Addrow)
        {
            if (str2 == null) { str2 = ""; }
            if (str2 == null) { return; }
            int index = dataGridView1.Rows.Count+1;
            str[0] = index.ToString();
            str[1] = DateTime.Now.ToLongTimeString();
            str[2] = str1;
            str[3] = str2+"";
            str[4] = str3;
            str[5] = str4;
            str[6] = str5;
            str[7] = "1";
            if (Addrow == false)
            {
                for (int tmp = 0; tmp < dataGridView1.RowCount; tmp++)
                {
                    if (dataGridView1[3, tmp].Value.ToString() == str2)
                    {
                        int count = int.Parse(dataGridView1[7, tmp].Value.ToString()) + 1;
                        dataGridView1[7, tmp].Value = count.ToString();
                        dataGridView1.Rows[tmp].Selected = true;
                        return;
                    }
                }
            }
            dataGridView1.Rows.Add(str);
            dataGridView1.Rows[index-1].Selected = true;
            dataGridView1.FirstDisplayedScrollingRowIndex = index-1;
        }
        private bool GetMultiID(ref string [] CardId)
        {
            int Count = 0;
            byte[] value = new byte[12800];
            //readerDLL.TagIds[] tagids = new readerDLL.TagIds[200];
            //for (int tmp = 0; tmp < tagids.Length; tmp++)
            //{
            //    tagids[tmp].TagID = new byte[12];
            //}
            short Result = readerDLL.MultipleTagIdentify(readerDLL.hComm, 7, ref Count, value );         //说明count:标签数量。value：读到的标签，格式为 A X B... X X X X A X B... X X X X A X B...........
            if (Result == 0)                                                                            //其中A为标签长度 为8时长度为8, 9长度为4，10长度为6，B为标签，每个标签长度（位数）受A影响
            {
                timer3.Enabled = true;
 
                int Length = GetTagLength(value[0]);
                int Startbit = 2;
                Array.Resize(ref CardId, Count);  
                for (int index = 0; index < Count; index++)
                {
                    CardId[index] = "";
                    for (int tmp = Startbit; tmp < Startbit + Length; tmp++)
                    {
                        CardId[index] = CardId[index] + value[tmp].ToString("X2"); //转为16进制
                    }
                    Startbit = Startbit +14;  //每张卡信息长度为14,卡ID长度为每个卡信息第一个字节决定
                    Length = GetTagLength(value[Startbit - 2]);
                }
                counttxt.Text = Count.ToString();
                return true;
            }
            return false;
         }
        private bool MultiIDCheck(bool Showcount)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                string[] CardId = new string[0];
                if (GetMultiID(ref CardId))
                {
                    if (CardId.Length > 0)
                    {
                        string str1 = Showcount ? Properties.Settings.Default.Setting22 + CardId.Length.ToString() : "";
                        for (int tmp = 0; tmp < CardId.Length; tmp++)
                        {
                            ShowToGrid(str1, CardId[tmp], "", "", "", Showcount);
                            str1 = "";
                            SaveToDadatabase(CardId[tmp]);
                        }
                    }
                    return true;
                }
                return false;
            }
            finally 
            {
                this.Cursor = Cursors.Default;
            }
        }
        private bool SaveToDadatabase(string cardno)
        {
            if (!inifile.save) { return true;}
            string sqlstr = null;
            if (inifile.onlyone)
            {
                sqlstr = "select top 1 cardno from cardno where cardno='"+ cardno +"'";
                OdbcDataReader rs = database.Datareader(sqlstr);
                if (rs != null)
                {
                    if (rs.Read()) 
                    {
                        rs.Dispose();
                        return true; 
                    }
                    rs.Dispose();
                }
            }
            sqlstr = "insert into cardno ([cardno],[date]) values('" + cardno  + "','" + DateTime.Now + "')";
            if (database.ExecuteNonQuery(sqlstr))
            {
                return true;
            }
            return false;
        }
        private void readini()
        {
           string tmp= inifile.Readini("set", "save");
           inifile.save =tmp.ToUpper() =="TRUE"?true :false ;
           tmp = inifile.Readini("set", "onlyone");
           inifile.onlyone= tmp.ToUpper() == "TRUE" ? true : false; 
        }
//***************************************************************************************
        private void button9_Click(object sender, EventArgs e)
        {
            if (button9.Tag.ToString() == "1")
            {
                button9.Tag = "0";
                button9.Text = Properties.Settings.Default.Setting35 + button4.Text;
                timer1.Enabled = false;
                button10.Enabled = true; 
            }
            else
            {
                button9.Tag = "1";
                button9.Text = Properties.Settings.Default.Setting36;
                button10.Enabled = false; 
                dataGridView1.RowCount = 0;
                MultiIDCheck(false);            //为了更容易查看实际检测情总，先获取一下所有标签;
                timer1.Enabled = true;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            byte TagType = 0;
            byte[] value = new byte[128];
            short Result = readerDLL.SingleTagIdentifyEX(readerDLL.hComm, ref TagType, value);
            if (Result == 0)
            {
                timer3.Enabled = true;

                if (TagType > 0)
                {
                    string CardId = "";
                    int Length = GetTagLength(TagType);
                    for (int tmp = 0; tmp < Length; tmp++)
                    {
                        CardId = CardId + value[tmp].ToString("X2");
                    }
                    ShowToGrid("", CardId, "", "", "", false);
                    SaveToDadatabase(CardId);
                }
            }
            if (button9.Tag.ToString() == "1") { timer1.Start(); }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (button10.Tag.ToString() == "1")
            {
                button10.Tag = "0";
                timer2.Enabled = false;
                button10.Text = Properties.Settings.Default.Setting35 + button5.Text;
                button9.Enabled = true;
            }
            else
            {
                button10.Tag = "1";
                button9.Enabled = false;
                button10.Text = Properties.Settings.Default.Setting36;
                dataGridView1.RowCount = 0;
                timer2.Enabled = true;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Stop();
            MultiIDCheck(false);
            if (button10.Tag.ToString() == "1") { timer2.Start(); }
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            commandtxt.Items.Clear(); 
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            dataGridView1.SelectionMode = checkBox1.Checked ? DataGridViewSelectionMode.FullRowSelect : DataGridViewSelectionMode.CellSelect;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) { return; }
            string str1 = dataGridView1[3, e.RowIndex].Value.ToString();
            textBox1.Text = str1.Substring(str1.Length - 8, 8);
            textBox6.Text = textBox1.Text;
            if (textBox1.Text.Trim() != "")
            {
                tabControl1.SelectedIndex = 1;
                textBox1.Focus();
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            frmselecttag frm = new frmselecttag(this);
            frm.Show(this);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmrecords frm = new frmrecords();
            frm.Show();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmset frm = new frmset();
            frm.ShowDialog(this);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            listView1.Refresh();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (button12.Tag.ToString() == "1")
            {
                button12.Tag = "0";
                timer4.Enabled = false;
                button12.Text = Properties.Settings.Default.Setting68;
            }
            else
            {
                if (listView1.Items.Count == 0)
                {
                    MessageBox.Show(Properties.Settings.Default.Setting75 + "  ");
                    button11.Focus(); 
                    return;
                }
                button12.Tag = "1";
                button12.Text = Properties.Settings.Default.Setting36;
                timer4.Enabled = true;
            }
        }
        private void showclear_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            LED.Visible = !LED.Visible; 
            timer3.Enabled = LED.Visible;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (textBox6.Text.Trim() == "") { return; }
            this.Cursor = Cursors.WaitCursor;
            int selrow = -1;
            if (dataGridView1.SelectionMode == DataGridViewSelectionMode.FullRowSelect)
            {
                if (dataGridView1.SelectedRows[0].Index >= 0)
                {
                    selrow = dataGridView1.SelectedRows[0].Index;
                }
            }
            else if (dataGridView1.SelectionMode == DataGridViewSelectionMode.CellSelect)
            {
                try
                {
                    if (dataGridView1.SelectedCells[0].RowIndex >= 0)
                    {
                        selrow = dataGridView1.SelectedCells[0].RowIndex;
                    }
                }
                catch { }
            }
            for (int row = selrow+1; row < dataGridView1.RowCount; row++)
            {
                if (dataGridView1[3, row].Value.ToString().IndexOf(textBox6.Text)>=0)
                {
                    dataGridView1[3, row].Selected = true;
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            for (int row = 0; row < selrow+1 ; row++)
            {
                if (dataGridView1[3, row].Value.ToString().IndexOf(textBox6.Text)>=0)
                {
                    dataGridView1[3, row].Selected = true;
                    this.Cursor = Cursors.Default;
                    return;
                }
            }
            this.Cursor = Cursors.Default;
            MessageBox.Show(Properties.Settings.Default.Setting76 + "   ", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            stopdo();   
        }
        private void stopdo()
        {
            if (button9.Tag.ToString() == "1")
            {
                button9_Click(null,null);
            }
            if (button10.Tag.ToString() == "1")
            {
                button10_Click(null, null);
            }
            if (button12.Tag.ToString() == "1")
            {
                button12_Click(null, null);
            }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            timer4.Stop(); 
            this.Cursor = Cursors.WaitCursor;
            string[] CardId = new string[0];
            if (GetMultiID(ref CardId))
            {
                if (CardId.Length > 0)
                {
                    for (int tmp = 0; tmp < CardId.Length; tmp++)
                    {
                        ListViewItem[] findstring = listView1.Items.Find(CardId[tmp] + "\t", false);
                        if (findstring.Length > 0)   //找到
                        {
                            listView1.Items[findstring[0].Index].ImageIndex = 1;
                            listView1.Items[findstring[0].Index].SubItems[1].Text = (int.Parse(listView1.Items[findstring[0].Index].SubItems[1].Text) + 1).ToString();
                            listView1.Items[findstring[0].Index].EnsureVisible();
                        }
                    }
                }
            }
            this.Cursor = Cursors.Default;
            if (button12.Tag.ToString() == "1") { timer4.Start(); }
        }

        private void clearcount_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor; 
            for (int row = 0; row < listView1.Items.Count; row++)
            {
                listView1.Items[row].SubItems[1].Text = "0";
            }
            this.Cursor = Cursors.Default;
        }
//*************************************************************************************
    }
}
