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

namespace DL580_9130
{
    public partial class frmselecttag : Form
    {
        main mefrm = null;
        private string[] str = new string[2];
        const int  buttoncol=1;

        public frmselecttag(main frm)
        {
            InitializeComponent();
            mefrm = frm;
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex != buttoncol) { return; }
            if (dataGridView1.CurrentRow.Cells[buttoncol].Value.ToString() == Properties.Settings.Default.Setting69)
            {
                dataGridView1.CurrentRow.Cells[buttoncol].Value = Properties.Settings.Default.Setting70;
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Bisque;
                AddToFind(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            }
            else
            {
                dataGridView1.CurrentRow.Cells[buttoncol].Value = Properties.Settings.Default.Setting69;
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = dataGridView1.BackgroundColor;
                int index = scand(dataGridView1.CurrentRow.Cells[0].Value.ToString() + "\t");
                if (index >= 0)
                {
                    mefrm.listView1.Items[index].Remove(); 
                }
            }
        }

        private void frmselecttag_Load(object sender, EventArgs e)
        {
            dataGridView1.Columns[0].HeaderText = Properties.Settings.Default.Setting2;
            dataGridView1.Columns[1].HeaderText = "";

            str[1] = Properties.Settings.Default.Setting69;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            dataGridView1.Rows.Clear();
            string sqlstr = "select distinct cardno  from cardno where [cardno] like '%" + textBox2.Text + "%'";
            OdbcDataReader rs = database.Datareader(sqlstr);
            if (rs == null) 
            {
                this.Cursor = Cursors.Default; 
                return; 
            }
            while (rs.Read())
            {
                str[0] = rs["cardno"].ToString().Trim();
                dataGridView1.Rows.Add(str);
            }
            rs.Dispose();
            this.Cursor = Cursors.Default; 
        }
        private int scand(string str)
        {
            ListViewItem[] findstring = mefrm.listView1.Items.Find(str, false);
            if (findstring.Length > 0)   //找到
            {
                return findstring[0].Index;
            }
            return -1;        
        }
        private void  AddToFind(string cardno)
        {
            int index=scand(cardno+"\t");
            if(index >=0)
            {
                mefrm.listView1.Items[index].EnsureVisible();
                return; 
            }
            mefrm.listView1.Items.Add(cardno  + "\t",cardno , 0);
            mefrm.listView1.Items[mefrm.listView1.Items.Count - 1].SubItems.Add("0");
            mefrm.listView1.Items[mefrm.listView1.Items.Count - 1].EnsureVisible();

        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != "") { AddToFind(textBox1.Text.Trim()); }
            textBox1.Focus();
            textBox1.SelectAll();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = "0123456789ABCDEF\b".IndexOf(char.ToUpper(e.KeyChar)) < 0;
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                button3_Click(sender, e);
            }
        }
    }
}
