using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; namespace UAS_MES_NEW.CustomControl.TextBoxWithIcon { public partial class BlurSearch : UserControl { DataHelper dh = SystemInf.dh; public delegate void OnTextChange(object sender, EventArgs e); public event OnTextChange UserControlTextChanged; 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 + "%' "; if (condition != "" && condition != null) { con = Field1 + " like '%" + EnterTextBox.Text + "%' 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 *10; 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) { } } } } private void EnterTextBox_Enter(object sender, EventArgs e) { string con = Field1 + " like '%" + EnterTextBox.Text + "%' "; if (condition != "" && condition != null) { con = Field1 + " like '%" + EnterTextBox.Text + "%' 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 * 10; 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; } } }