using System; using System.Data; using System.Windows.Forms; using UAS_LabelMachine; namespace UAS_LabelMachine.CustomControl { public partial class BlurSearch : UserControl { DataHelper dh = new DataHelper(); public delegate void OnTextChange(object sender, EventArgs e); public delegate void OnKeyDown(object sender, KeyEventArgs e); public event OnTextChange UserControlTextChanged; public event OnKeyDown UserKeyDown; string TableName1; string Field1; int ItemSelectIndex = 0; DataTable dt; public override string Text { get { return EnterTextBox.Text; } set { EnterTextBox.Text = value; } } public BlurSearch() { InitializeComponent(); } public string TableName { get { return TableName1; } set { TableName1 = value; } } private string value; public string Field { get { return Field1; } set { Field1 = value; } } public string Condition { get { return condition; } set { condition = value; } } public string ValueField { get { return valueField; } set { valueField = value; } } public string Value { get { return value; } set { this.value = value; } } private string valueField; private string condition; private void TextBox_Leave(object sender, EventArgs e) { Height = EnterTextBox.Height; ListBox.Visible = false; } private void BlurSearch_Load(object sender, EventArgs e) { Height = EnterTextBox.Height; ListBox.Visible = false; } private void EnterTextBox_TextChanged(object sender, EventArgs e) { if (TableName1 != null && EnterTextBox.Focused) { string con = Field1 + " like '%" + EnterTextBox.Text + "%' and rownum<=5 "; if (condition != "" && condition != null) { con = Field1 + " like '%" + EnterTextBox.Text + "%' and rownum<=5 and " + condition; } dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1, valueField, "rownum" }, con.ToUpper()); if (dt.Rows.Count > 0) { ListBox.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { ListBox.Items.Add(dt.Rows[i][Field1]); } //每次数据查询之后将索引重置 ItemSelectIndex = 0; ListBox.Height = ListBox.ItemHeight * (dt.Rows.Count + 1); ListBox.Visible = true; Height = ListBox.Height + EnterTextBox.Height; } if (dt.Rows.Count == 1) { value = dt.Rows[0][valueField].ToString(); } } else { ListBox.Visible = false; Height = EnterTextBox.Height; } UserControlTextChanged?.Invoke(sender, new EventArgs()); } private void ListBox_Click(object sender, EventArgs e) { value = dt.Select("rownum=" + (ListBox.SelectedIndex + 1))[0][1].ToString(); EnterTextBox.Text = ListBox.SelectedItem.ToString(); ListBox.Visible = false; Height = EnterTextBox.Height; } private void EnterTextBox_KeyDown(object sender, KeyEventArgs e) { if (ListBox.Items.Count > 0) { if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) { switch (e.KeyCode) { case Keys.Down: if (ListBox.Items.Count > ItemSelectIndex + 1) ItemSelectIndex = ItemSelectIndex + 1; break; case Keys.Up: if (ItemSelectIndex - 1 >= 0) ItemSelectIndex = ItemSelectIndex - 1; break; default: break; } ListBox.SelectedIndex = ItemSelectIndex; } if (e.KeyCode == Keys.Enter) { try { EnterTextBox.Text = ListBox.SelectedItem.ToString(); ListBox.Visible = false; Height = EnterTextBox.Height; } catch (Exception) { } } } UserKeyDown?.Invoke(sender, e); } private void EnterTextBox_Enter(object sender, EventArgs e) { string con = Field1 + " like '%" + EnterTextBox.Text + "%' and rownum<=10 "; if (condition != "" && condition != null) { con = Field1 + " like '%" + EnterTextBox.Text + "%' and rownum<=10 and " + condition; } dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1, valueField, "rownum" }, con.ToUpper()); if (dt.Rows.Count > 0) { ListBox.Items.Clear(); for (int i = 0; i < dt.Rows.Count; i++) { ListBox.Items.Add(dt.Rows[i][Field1]); } //每次数据查询之后将索引重置 ItemSelectIndex = 0; ListBox.Height = ListBox.ItemHeight * (dt.Rows.Count + 1); ListBox.Visible = true; Height = ListBox.Height + EnterTextBox.Height; } } private void EnterTextBox_Leave(object sender, EventArgs e) { //ListBox.Visible = false; } private void BlurSearch_Leave(object sender, EventArgs e) { ListBox.Height = 0; ListBox.Visible = false; Height = ListBox.Height + EnterTextBox.Height; } } }