版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

120 строки
4.2 KiB

  1. using ProductionControl.UIExtend;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace ProductionControl
  12. {
  13. public partial class FrmInput : Form
  14. {
  15. private TextBoxRemind remind = null;
  16. private string[] listData;
  17. public string inputData = "";
  18. public FrmInput(List<string> list, string subject = null,string defaultData="")
  19. {
  20. InitializeComponent();
  21. if (list == null)
  22. {
  23. this.cobList.Visible = false;
  24. this.textBox1.Text = defaultData;
  25. remind = new TextBoxRemind();
  26. remind.InitAutoCompleteCustomSource(textBox1);
  27. }
  28. else
  29. {
  30. this.textBox1.Visible = false;
  31. this.cobList.DropDownStyle = ComboBoxStyle.DropDown;//可输入下拉框
  32. //this.cobList.DataSource = list;
  33. //cobList.AutoCompleteSource = AutoCompleteSource.ListItems;
  34. //cobList.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
  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 (textBox1.Visible)
  58. {
  59. inputData = this.textBox1.Text.Trim();
  60. if (inputData.Trim() == "") return;
  61. //记忆
  62. remind.Remind(inputData);
  63. }
  64. else
  65. {
  66. if (cobList.Items.Count <1)
  67. return;
  68. string tmp = this.cobList.Text.Trim();
  69. var text = listData.FirstOrDefault(x => x== tmp);//忽略大小写
  70. if (string.IsNullOrEmpty(text))
  71. return;
  72. inputData = text;
  73. }
  74. this.DialogResult = DialogResult.OK;
  75. }
  76. private void cobList_TextUpdate(object sender, EventArgs e)
  77. {
  78. string str = cobList.Text; //获取cb_material控件输入内
  79. //清空combobox
  80. cobList.DataSource = null;
  81. cobList.Items.Clear();
  82. var workOrderFiltered = listData.Where(x => x.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();//忽略大小写
  83. cobList.Items.AddRange(workOrderFiltered);//比使用DataSource速度要快一些
  84. // 不存在符合条件时
  85. //设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
  86. cobList.Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置
  87. if (workOrderFiltered.Length > 0)
  88. {
  89. if (!cobList.DroppedDown)
  90. cobList.DroppedDown = true; // 自动弹出下拉框
  91. }
  92. //else if (cobList.DroppedDown)
  93. // cobList.DroppedDown = false;
  94. cobList.SelectionStart = str.Length; // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
  95. }
  96. private void FrmInput_Load(object sender, EventArgs e)
  97. {
  98. }
  99. private void FrmInput_Shown(object sender, EventArgs e)
  100. {
  101. if(textBox1.Visible)
  102. textBox1.Focus();
  103. }
  104. }
  105. }