版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

InputFrm.cs 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace MaiMuAOI.SysUI.ProductAndStep
  11. {
  12. public partial class InputFrm : Form
  13. {
  14. private string[] listData;
  15. public string inputData = "";
  16. public InputFrm(List<string> list, string subject = null, string defaultData = "")
  17. {
  18. InitializeComponent();
  19. UIStyle.SetUIStyle(this);
  20. this.uiTitel1.FatherForm = this;
  21. if (list == null)
  22. {
  23. this.cobList.Visible = false;
  24. this.tbInputSN.Visible = true;
  25. this.tbInputSN.Focus();
  26. this.tbInputSN.Select();
  27. this.label1.Text = subject;
  28. }
  29. else
  30. {
  31. this.label1.Text = subject;
  32. this.cobList.Visible = true;
  33. this.tbInputSN.Visible = false;
  34. this.cobList.DropDownStyle = ComboBoxStyle.DropDown;//可输入下拉框
  35. this.listData = list.ToArray();
  36. cobList.Items.AddRange(listData);//比使用DataSource速度要快一些
  37. cobList.TextUpdate += cobList_TextUpdate;//重新绑定事件
  38. cobList.KeyDown += CobList_KeyDown;
  39. this.cobList.Text = defaultData;
  40. cobList.Focus();
  41. cobList.SelectAll();
  42. }
  43. //
  44. if (!string.IsNullOrWhiteSpace(subject))
  45. this.Text = subject;
  46. }
  47. private void CobList_KeyDown(object sender, KeyEventArgs e)
  48. {
  49. if (e.KeyCode == Keys.Enter)
  50. {
  51. if (cobList.Items.Count == 1)
  52. cobList.Text = cobList.Items[0].ToString();
  53. }
  54. }
  55. private void btnOK_Click(object sender, EventArgs e)
  56. {
  57. if (this.tbInputSN.Visible == true)
  58. {
  59. inputData = tbInputSN.Text;
  60. }
  61. else
  62. {
  63. if (cobList.Items.Count < 1)
  64. return;
  65. string tmp = this.cobList.Text.Trim();
  66. var text = listData.FirstOrDefault(x => x == tmp);//忽略大小写
  67. if (string.IsNullOrEmpty(text))
  68. return;
  69. inputData = text;
  70. }
  71. this.DialogResult = DialogResult.OK;
  72. }
  73. private void cobList_TextUpdate(object sender, EventArgs e)
  74. {
  75. string str = cobList.Text; //获取cb_material控件输入内
  76. //清空combobox
  77. cobList.DataSource = null;
  78. cobList.Items.Clear();
  79. var workOrderFiltered = listData.Where(x => x.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();//忽略大小写
  80. cobList.Items.AddRange(workOrderFiltered);//比使用DataSource速度要快一些
  81. // 不存在符合条件时
  82. //设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
  83. cobList.Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置
  84. if (workOrderFiltered.Length > 0)
  85. {
  86. if (!cobList.DroppedDown)
  87. cobList.DroppedDown = true; // 自动弹出下拉框
  88. }
  89. cobList.SelectionStart = str.Length; // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
  90. }
  91. private void btnCancel_Click(object sender, EventArgs e)
  92. {
  93. this.DialogResult = DialogResult.Cancel;
  94. }
  95. }
  96. }