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.DataOperate; namespace UAS_MES.CustomControl.TextBoxWithIcon { public partial class BlurSearch : UserControl { DataHelper dh = new DataHelper(); string TableName1; string Field1; int ItemSelectIndex = -1; public override string Text { get { return EnterTextBox.Text; } set { EnterTextBox.Text = value; } } public BlurSearch() { InitializeComponent(); } public string TableName { get { return TableName1; } set { TableName1 = value; } } public string Field { get { return Field1; } set { Field1 = value; } } 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 (EnterTextBox.Text != "") { DataTable dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1 }, Field1 + " like '%" + EnterTextBox.Text + "%' and rownum<=10"); 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 = -1; ListBox.Height = ListBox.ItemHeight * dt.Rows.Count; ListBox.Visible = true; Height = ListBox.Height + EnterTextBox.Height; } } else { ListBox.Visible = false; Height = EnterTextBox.Height; } } private void ListBox_Click(object sender, EventArgs e) { 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) { } } } } } }