using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Threading;

namespace FPDemo
{
    public partial class FormUserEnrol : Form
    {
        public int refsize1;
        public byte[] refbuf1;
        public System.Drawing.Bitmap fingerbmp1;

        public int refsize2;
        public byte[] refbuf2;
        public System.Drawing.Bitmap fingerbmp2;

        public int refsize3;
        public byte[] refbuf3;
        public System.Drawing.Bitmap fingerbmp3;

        public int refsize4;
        public byte[] refbuf4;
        public System.Drawing.Bitmap fingerbmp4;

        public int refsize5;
        public byte[] refbuf5;
        public System.Drawing.Bitmap fingerbmp5;

        public int refsize6;
        public byte[] refbuf6;
        public System.Drawing.Bitmap fingerbmp6;

        public int refsize7;
        public byte[] refbuf7;
        public System.Drawing.Bitmap fingerbmp7;

        public int refsize8;
        public byte[] refbuf8;
        public System.Drawing.Bitmap fingerbmp8;

        public int refsize9;
        public byte[] refbuf9;
        public System.Drawing.Bitmap fingerbmp9;

        public int refsize10;
        public byte[] refbuf10;
        public System.Drawing.Bitmap fingerbmp10;

        public FormUserEnrol()
        {
            InitializeComponent();

            refsize1 = 0;
            refbuf1 = new byte[512];
            fingerbmp1 = new Bitmap(255, 288);

            refsize2 = 0;
            refbuf2 = new byte[512];
            fingerbmp2 = new Bitmap(255, 288);

            refsize3 = 0;
            refbuf3 = new byte[512];
            fingerbmp3 = new Bitmap(255, 288);

            refsize4 = 0;
            refbuf4 = new byte[512];
            fingerbmp4 = new Bitmap(255, 288);

            refsize5 = 0;
            refbuf5 = new byte[512];
            fingerbmp5 = new Bitmap(255, 288);

            refsize6 = 0;
            refbuf6 = new byte[512];
            fingerbmp6 = new Bitmap(255, 288);

            refsize7 = 0;
            refbuf7 = new byte[512];
            fingerbmp7 = new Bitmap(255, 288);

            refsize8 = 0;
            refbuf8 = new byte[512];
            fingerbmp8 = new Bitmap(255, 288);

            refsize9 = 0;
            refbuf9 = new byte[512];
            fingerbmp9 = new Bitmap(255, 288);

            refsize10 = 0;
            refbuf10 = new byte[512];
            fingerbmp10 = new Bitmap(255, 288);
        }

        public void FormUserEnrol_CheckClick(object sender, EventArgs e)
        {
            CheckBox ckb;
            ckb = sender as CheckBox;

            FormFingerEnrol ffe = new FormFingerEnrol();
            if(ffe.ShowDialog()==DialogResult.OK)
            {
                ckb.Checked = true;
                if (ckb == checkBox1)
                {
                    refsize1 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf1, 0);
                    fingerbmp1 = ffe.fingerbmp;
                    pbImage1.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox2)
                {
                    refsize2 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf2, 0);
                    fingerbmp2 = ffe.fingerbmp;
                    pbImage2.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox3)
                {
                    refsize3 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf3, 0);
                    fingerbmp3 = ffe.fingerbmp;
                    pbImage3.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox4)
                {
                    refsize4 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf4, 0);
                    fingerbmp4 = ffe.fingerbmp;
                    pbImage4.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox5)
                {
                    refsize5 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf5, 0);
                    fingerbmp5 = ffe.fingerbmp;
                    pbImage5.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox6)
                {
                    refsize6 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf6, 0);
                    fingerbmp6 = ffe.fingerbmp;
                    pbImage6.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox7)
                {
                    refsize7 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf7, 0);
                    fingerbmp7 = ffe.fingerbmp;
                    pbImage7.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox8)
                {
                    refsize8 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf8, 0);
                    fingerbmp8 = ffe.fingerbmp;
                    pbImage8.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox9)
                {
                    refsize9 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf9, 0);
                    fingerbmp9 = ffe.fingerbmp;
                    pbImage9.Image = ffe.fingerbmp;
                }
                else if (ckb == checkBox10)
                {
                    refsize10 = ffe.refsize;
                    ffe.refbuf.CopyTo(refbuf10, 0);
                    fingerbmp10 = ffe.fingerbmp;
                    pbImage10.Image = ffe.fingerbmp;
                }
            }
            else
            {
                ckb.Checked = false;
                if (ckb == checkBox1)
                {
                    refsize1 = 0;
                    fingerbmp1 = null;
                    pbImage1.Image = null;
                }
                else if (ckb == checkBox2)
                {
                    refsize2 = 0;
                    fingerbmp2 = null;
                    pbImage2.Image = null;
                }
                else if (ckb == checkBox3)
                {
                    refsize3 = 0;
                    fingerbmp3 = null;
                    pbImage3.Image = null;
                }
                else if (ckb == checkBox4)
                {
                    refsize4 = 0;
                    fingerbmp4 = null;
                    pbImage4.Image = null;
                }
                else if (ckb == checkBox5)
                {
                    refsize5 = 0;
                    fingerbmp5 = null;
                    pbImage5.Image = null;
                }
                else if (ckb == checkBox6)
                {
                    refsize6 = 0;
                    fingerbmp6 = null;
                    pbImage6.Image = null;
                }
                else if (ckb == checkBox7)
                {
                    refsize7 = 0;
                    fingerbmp7 = null;
                    pbImage7.Image = null;
                }
                else if (ckb == checkBox8)
                {
                    refsize8 = 0;
                    fingerbmp8 = null;
                    pbImage8.Image = null;
                }
                else if (ckb == checkBox9)
                {
                    refsize9 = 0;
                    fingerbmp9 = null;
                    pbImage9.Image = null;
                }
                else if (ckb == checkBox10)
                {
                    refsize10 = 0;
                    fingerbmp10 = null;
                    pbImage10.Image = null;
                }
            }
        }

        private void FormUserEnrol_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“fpdbDataSet1.fplist”中。您可以根据需要移动或删除它。
            this.fplistTableAdapter1.Fill(this.fpdbDataSet1.fplist);
            // TODO: This line of code loads data into the 'fpdbDataSet.fplist' table. You can move, or remove it, as needed.
            this.fplistTableAdapter1.Fill(this.fpdbDataSet1.fplist);

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.fpdbDataSet1.fplist.Rows.Count > 0)
            {
                this.fpdbDataSet1.fplist.Rows[this.bindingSource1.Position].Delete();
                this.fplistTableAdapter1.Update(this.fpdbDataSet1.fplist);
            }    
        }

        private void buttonAppend_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                MessageBox.Show("Please input user ID","Hint");
                return;
            }

            if(checkBox11.Checked==true)
            {
                DataRow fr;
                fr = this.fpdbDataSet1.fplist.Rows.Find(textBox1.Text);
                if (fr == null)
                {
                    MessageBox.Show("User ID does not exist,not save", "Hint");
                    return;
                }
                else
                {
                }

                DataRow dr = this.fpdbDataSet1.fplist.Rows[dataGridView1.CurrentCell.RowIndex];

                dr.BeginEdit();
                
                dr[0] = textBox1.Text;
                dr[1] = textBox2.Text;
                dr[9] = dateTimePicker1.Text;
                dr[2] = textBox3.Text;
                dr[4] = textBox4.Text;
                dr[5] = textBox5.Text;

                if (refsize1 > 0)
                {
                    dr[12] = refbuf1;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp1.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[22] = bitmapData;
                }
                else
                {
                    dr[12] = null;
                    dr[22] = null;
                }

                if (refsize2 > 0)
                {
                    dr[13] = refbuf2;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp2.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[23] = bitmapData;
                }
                else
                {
                    dr[13] = null;
                    dr[23] = null;
                }

                if (refsize3 > 0)
                {
                    dr[14] = refbuf3;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp3.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[24] = bitmapData;
                }
                else
                {
                    dr[14] = null;
                    dr[24] = null;
                }

                if (refsize4 > 0)
                {
                    dr[15] = refbuf4;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp4.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[25] = bitmapData;
                }
                else
                {
                    dr[15] = null;
                    dr[25] = null;
                }

                if (refsize5 > 0)
                {
                    dr[16] = refbuf5;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp5.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[26] = bitmapData;
                }
                else
                {
                    dr[16] = null;
                    dr[26] = null;
                }

                if (refsize6 > 0)
                {
                    dr[17] = refbuf6;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp6.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[27] = bitmapData;
                }
                else
                {
                    dr[17] = null;
                    dr[27] = null;
                }

                if (refsize7 > 0)
                {
                    dr[18] = refbuf7;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp7.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[28] = bitmapData;
                }
                else
                {
                    dr[18] = null;
                    dr[28] = null;
                }

                if (refsize8 > 0)
                {
                    dr[19] = refbuf8;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp8.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[29] = bitmapData;
                }
                else
                {
                    dr[19] = null;
                    dr[29] = null;
                }

                if (refsize9 > 0)
                {
                    dr[20] = refbuf9;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp9.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[30] = bitmapData;
                }
                else
                {
                    dr[20] = null;
                    dr[30] = null;
                }

                if (refsize10 > 0)
                {
                    dr[21] = refbuf10;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp10.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[31] = bitmapData;
                }
                else
                {
                    dr[21] = null;
                    dr[31] = null;
                }

                dr.EndEdit();

                this.fplistTableAdapter1.Update(this.fpdbDataSet1.fplist);

            }
            else
            {
                //Append
                DataRow fr;
                fr = this.fpdbDataSet1.fplist.Rows.Find(textBox1.Text);
                if (fr == null)
                {
                }
                else
                {
                    MessageBox.Show("User ID is exist,Plese re-input", "Hint");
                    return;
                }

                DataRow dr = this.fpdbDataSet1.fplist.NewRow();
                dr[0] = textBox1.Text;
                dr[1] = textBox2.Text;
                dr[9] = dateTimePicker1.Text;
                dr[2] = textBox3.Text;
                dr[4] = textBox4.Text;
                dr[5] = textBox5.Text;

                if (refsize1 > 0)
                {
                    dr[12] = refbuf1;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp1.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[22] = bitmapData;
                }
                if (refsize2 > 0)
                {
                    dr[13] = refbuf2;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp2.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[23] = bitmapData;
                }
                if (refsize3 > 0)
                {
                    dr[14] = refbuf3;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp3.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[24] = bitmapData;
                }
                if (refsize4 > 0)
                {
                    dr[15] = refbuf4;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp4.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[25] = bitmapData;
                }
                if (refsize5 > 0)
                {
                    dr[16] = refbuf5;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp5.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[26] = bitmapData;
                }
                if (refsize6 > 0)
                {
                    dr[17] = refbuf6;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp6.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[27] = bitmapData;
                }
                if (refsize7 > 0)
                {
                    dr[18] = refbuf7;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp7.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[28] = bitmapData;
                }
                if (refsize8 > 0)
                {
                    dr[19] = refbuf8;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp8.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[29] = bitmapData;
                }
                if (refsize9 > 0)
                {
                    dr[20] = refbuf9;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp9.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[30] = bitmapData;
                }
                if (refsize10 > 0)
                {
                    dr[21] = refbuf10;
                    MemoryStream ms = new MemoryStream();
                    fingerbmp10.Save(ms, ImageFormat.Bmp);
                    byte[] bitmapData = ms.ToArray();
                    dr[31] = bitmapData;
                }

                this.fpdbDataSet1.fplist.Rows.Add(dr);
                this.fplistTableAdapter1.Update(this.fpdbDataSet1.fplist);

                //null
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;
                checkBox6.Checked = false;
                checkBox7.Checked = false;
                checkBox8.Checked = false;
                checkBox9.Checked = false;
                checkBox10.Checked = false;

                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";

                pbImage1.Image = null;
                pbImage2.Image = null;
                pbImage3.Image = null;
                pbImage4.Image = null;
                pbImage5.Image = null;
                pbImage6.Image = null;
                pbImage7.Image = null;
                pbImage8.Image = null;
                pbImage9.Image = null;
                pbImage10.Image = null;

                refsize1 = 0;
                refsize2 = 0;
                refsize3 = 0;
                refsize4 = 0;
                refsize5 = 0;
                refsize6 = 0;
                refsize7 = 0;
                refsize8 = 0;
                refsize9 = 0;
                refsize10 = 0;
            }
        }

        private void bindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if(checkBox11.Checked==true)
            {
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;
                checkBox6.Checked = false;
                checkBox7.Checked = false;
                checkBox8.Checked = false;
                checkBox9.Checked = false;
                checkBox10.Checked = false;

                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";

                pbImage1.Image = null;
                pbImage2.Image = null;
                pbImage3.Image = null;
                pbImage4.Image = null;
                pbImage5.Image = null;
                pbImage6.Image = null;
                pbImage7.Image = null;
                pbImage8.Image = null;
                pbImage9.Image = null;
                pbImage10.Image = null;

                refsize1 = 0;
                refsize2 = 0;
                refsize3 = 0;
                refsize4 = 0;
                refsize5 = 0;
                refsize6 = 0;
                refsize7 = 0;
                refsize8 = 0;
                refsize9 = 0;
                refsize10 = 0;

                //DataRow[] Rows = this.fpdbDataSet.fplist.Select();
                if(dataGridView1.CurrentCell.RowIndex<fpdbDataSet1.fplist.Rows.Count)
                {
                    DataRow cr = this.fpdbDataSet1.fplist.Rows[dataGridView1.CurrentCell.RowIndex];
                    //DataRow cr = Rows[dataGridView1.CurrentCell.RowIndex];
                    textBox1.Text = cr.ItemArray.GetValue(0).ToString();
                    textBox2.Text = cr.ItemArray.GetValue(1).ToString();
                    dateTimePicker1.Text = cr.ItemArray.GetValue(9).ToString();
                    textBox3.Text = cr.ItemArray.GetValue(2).ToString();
                    textBox4.Text = cr.ItemArray.GetValue(4).ToString();
                    textBox5.Text = cr.ItemArray.GetValue(5).ToString();

                    if (cr[12] != DBNull.Value)
                    {
                        refbuf1 = (byte[])cr[12];
                        refsize1 = refbuf1.GetLength(0);

                        byte[] bitmapData = (byte[])cr[22];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp1 = image;
                        pbImage1.Image = image;

                        checkBox1.Checked = true;
                    }
                    if (cr[13] != DBNull.Value)
                    {
                        refbuf2 = (byte[])cr[13];
                        refsize2 = refbuf2.GetLength(0);

                        byte[] bitmapData = (byte[])cr[23];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp2 = image;
                        pbImage2.Image = image;

                        checkBox2.Checked = true;
                    }
                    if (cr[14] != DBNull.Value)
                    {
                        refbuf3 = (byte[])cr[14];
                        refsize3 = refbuf3.GetLength(0);

                        byte[] bitmapData = (byte[])cr[24];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp3 = image;
                        pbImage3.Image = image;

                        checkBox3.Checked = true;
                    }
                    
                    if (cr[15] != DBNull.Value)
                    {
                        refbuf4 = (byte[])cr[15];
                        refsize4 = refbuf4.GetLength(0);

                        byte[] bitmapData = (byte[])cr[25];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp4 = image;
                        pbImage4.Image = image;

                        checkBox4.Checked = true;
                    }
                    
                    if (cr[16] != DBNull.Value)
                    {
                        refbuf5 = (byte[])cr[16];
                        refsize5 = refbuf5.GetLength(0);

                        byte[] bitmapData = (byte[])cr[26];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp5 = image;
                        pbImage5.Image = image;

                        checkBox5.Checked = true;
                    }
                    
                    if (cr[17] != DBNull.Value)
                    {
                        refbuf6 = (byte[])cr[17];
                        refsize6 = refbuf6.GetLength(0);

                        byte[] bitmapData = (byte[])cr[27];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp6 = image;
                        pbImage6.Image = image;

                        checkBox6.Checked = true;
                    }
                    
                    if (cr[18] != DBNull.Value)
                    {
                        refbuf7 = (byte[])cr[18];
                        refsize7 = refbuf7.GetLength(0);

                        byte[] bitmapData = (byte[])cr[28];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp7 = image;
                        pbImage7.Image = image;

                        checkBox7.Checked = true;
                    }
                 
                    if (cr[19] != DBNull.Value)
                    {
                        refbuf8 = (byte[])cr[19];
                        refsize8 = refbuf8.GetLength(0);

                        byte[] bitmapData = (byte[])cr[29];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp8 = image;
                        pbImage8.Image = image;

                        checkBox8.Checked = true;
                    }
                    
                    if (cr[20] != DBNull.Value)
                    {
                        refbuf9 = (byte[])cr[20];
                        refsize9 = refbuf9.GetLength(0);

                        byte[] bitmapData = (byte[])cr[30];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp9 = image;
                        pbImage9.Image = image;

                        checkBox9.Checked = true;
                    }
                    
                    if (cr[21] != DBNull.Value)
                    {
                        refbuf10 = (byte[])cr[21];
                        refsize10 = refbuf10.GetLength(0);

                        byte[] bitmapData = (byte[])cr[31];
                        MemoryStream ms = new MemoryStream(bitmapData);
                        Bitmap image = new Bitmap(ms);
                        fingerbmp10 = image;
                        pbImage10.Image = image;

                        checkBox10.Checked = true;
                    }                    
                    
                }
            }
        }

        private void checkBox11_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox11.Checked==true)
            {
                buttonAppend.Text="&Save to database";
                textBox1.Enabled = false;
            }
            else
            {
                buttonAppend.Text = "&Add to Database";
                textBox1.Enabled = true;

                //null
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                checkBox4.Checked = false;
                checkBox5.Checked = false;
                checkBox6.Checked = false;
                checkBox7.Checked = false;
                checkBox8.Checked = false;
                checkBox9.Checked = false;
                checkBox10.Checked = false;

                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";

                pbImage1.Image = null;
                pbImage2.Image = null;
                pbImage3.Image = null;
                pbImage4.Image = null;
                pbImage5.Image = null;
                pbImage6.Image = null;
                pbImage7.Image = null;
                pbImage8.Image = null;
                pbImage9.Image = null;
                pbImage10.Image = null;

                refsize1 = 0;
                refsize2 = 0;
                refsize3 = 0;
                refsize4 = 0;
                refsize5 = 0;
                refsize6 = 0;
                refsize7 = 0;
                refsize8 = 0;
                refsize9 = 0;
                refsize10 = 0;
            }
        }

        private void buttonIdentify1_Click(object sender, EventArgs e)
        {
            
            if(dataGridView1.CurrentCell.RowIndex>=fpdbDataSet1.fplist.Rows.Count)
                 return;

            FormFingerIdentify ffi = new FormFingerIdentify();
            if (ffi.ShowDialog() == DialogResult.OK)
            {
                //ffi.matsize;
                //ffi.matbuf;
                DataRow cr = this.fpdbDataSet1.fplist.Rows[dataGridView1.CurrentCell.RowIndex];
                
                for(Int32 i=12;i<22;i++)
                {
                    if (cr[i] != DBNull.Value)
                    {
                        byte[] refbuf = (byte[])cr[i];
                        int refsize = refbuf.GetLength(0);

                        if(fpengine.MatchTemplateOne(ffi.matbuf, refbuf, refsize)>100)
                        {
                            String txt;
                            txt = "Succeed>>ID:" + cr.ItemArray.GetValue(0).ToString() + " Name:" + cr.ItemArray.GetValue(1).ToString() + " Finger Number:" + (i - 11).ToString();

                            MessageBox.Show(txt, "Validate User");
                            return;
                        }
                    }
                }

                MessageBox.Show("Fail", "Validate User");
                return;
            }            
        }

        private void buttonIdentifyn_Click(object sender, EventArgs e)
        {
            if(dataGridView1.CurrentCell.RowIndex>=fpdbDataSet1.fplist.Rows.Count)
                 return;

            FormFingerIdentify ffi = new FormFingerIdentify();
            if (ffi.ShowDialog() == DialogResult.OK)
            {

                for (int row = 0; row < fpdbDataSet1.fplist.Rows.Count; row++)
                {
                    DataRow cr = this.fpdbDataSet1.fplist.Rows[row];

                    for (Int32 i = 12; i < 22; i++)
                    {
                        if (cr[i] != DBNull.Value)
                        {
                            byte[] refbuf = (byte[])cr[i];
                            int refsize = refbuf.GetLength(0);

                            //if (fpengine.MatchTemplateOne(ffi.matbuf, refbuf, refsize) >=80)
                            if (fpengine.MatchTemplate(ffi.matbuf, refbuf) >= 80)
                            {
                                String txt;
                                txt = "Succeed>>ID:" + cr.ItemArray.GetValue(0).ToString() + " Name:" + cr.ItemArray.GetValue(1).ToString() + " Finger Number:" + (i - 11).ToString();

                                MessageBox.Show(txt, "Search Users");
                                return;
                            }
                        }
                    }
                }

                MessageBox.Show("Fail", "Search Users");
                return;
            } 
        }

        private void buttonRed_Click(object sender, EventArgs e)
        {
           
        }

        private void buttonGreen_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonWriteInfo_Click(object sender, EventArgs e)
        {
           
        }

        private void buttonReadInfo_Click(object sender, EventArgs e)
        {
        }

        
    }
}