123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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)
- {
- }
- }
- }
- }
- }
- }
|