123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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;
- }
- }
- }
|