BlurSearch.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using UAS_MES.DataOperate;
  10. namespace UAS_MES.CustomControl.TextBoxWithIcon
  11. {
  12. public partial class BlurSearch : UserControl
  13. {
  14. DataHelper dh = new DataHelper();
  15. string TableName1;
  16. string Field1;
  17. int ItemSelectIndex = -1;
  18. public override string Text
  19. {
  20. get
  21. {
  22. return EnterTextBox.Text;
  23. }
  24. set
  25. {
  26. EnterTextBox.Text = value;
  27. }
  28. }
  29. public BlurSearch()
  30. {
  31. InitializeComponent();
  32. }
  33. public string TableName
  34. {
  35. get
  36. {
  37. return TableName1;
  38. }
  39. set
  40. {
  41. TableName1 = value;
  42. }
  43. }
  44. public string Field
  45. {
  46. get
  47. {
  48. return Field1;
  49. }
  50. set
  51. {
  52. Field1 = value;
  53. }
  54. }
  55. private void TextBox_Leave(object sender, EventArgs e)
  56. {
  57. Height = EnterTextBox.Height;
  58. ListBox.Visible = false;
  59. }
  60. private void BlurSearch_Load(object sender, EventArgs e)
  61. {
  62. Height = EnterTextBox.Height;
  63. ListBox.Visible = false;
  64. }
  65. private void EnterTextBox_TextChanged(object sender, EventArgs e)
  66. {
  67. if (EnterTextBox.Text != "")
  68. {
  69. DataTable dt = dh.getFieldsDatasByCondition(TableName1, new string[] { Field1 }, Field1 + " like '%" + EnterTextBox.Text + "%' and rownum<=10");
  70. if (dt.Rows.Count > 0)
  71. {
  72. ListBox.Items.Clear();
  73. for (int i = 0; i < dt.Rows.Count; i++)
  74. {
  75. ListBox.Items.Add(dt.Rows[i][Field1]);
  76. }
  77. //每次数据查询之后将索引重置
  78. ItemSelectIndex = -1;
  79. ListBox.Height = ListBox.ItemHeight * dt.Rows.Count;
  80. ListBox.Visible = true;
  81. Height = ListBox.Height + EnterTextBox.Height;
  82. }
  83. }
  84. else
  85. {
  86. ListBox.Visible = false;
  87. Height = EnterTextBox.Height;
  88. }
  89. }
  90. private void ListBox_Click(object sender, EventArgs e)
  91. {
  92. EnterTextBox.Text = ListBox.SelectedItem.ToString();
  93. ListBox.Visible = false;
  94. Height = EnterTextBox.Height;
  95. }
  96. private void EnterTextBox_KeyDown(object sender, KeyEventArgs e)
  97. {
  98. if (ListBox.Items.Count > 0)
  99. {
  100. if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
  101. {
  102. switch (e.KeyCode)
  103. {
  104. case Keys.Down:
  105. if (ListBox.Items.Count > ItemSelectIndex + 1)
  106. ItemSelectIndex = ItemSelectIndex + 1;
  107. break;
  108. case Keys.Up:
  109. if (ItemSelectIndex - 1 >= 0)
  110. ItemSelectIndex = ItemSelectIndex - 1;
  111. break;
  112. default:
  113. break;
  114. }
  115. ListBox.SelectedIndex = ItemSelectIndex;
  116. }
  117. if (e.KeyCode == Keys.Enter)
  118. {
  119. try
  120. {
  121. EnterTextBox.Text = ListBox.SelectedItem.ToString();
  122. ListBox.Visible = false;
  123. Height = EnterTextBox.Height;
  124. }
  125. catch (Exception)
  126. {
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }