using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace MaiMuAOI.SysUI.ProductAndStep { public partial class InputFrm : Form { private string[] listData; public string inputData = ""; public InputFrm(List list, string subject = null, string defaultData = "") { InitializeComponent(); UIStyle.SetUIStyle(this); this.uiTitel1.FatherForm = this; if (list == null) { this.cobList.Visible = false; this.tbInputSN.Visible = true; this.tbInputSN.Focus(); this.tbInputSN.Select(); this.label1.Text = subject; } else { this.label1.Text = subject; this.cobList.Visible = true; this.tbInputSN.Visible = false; this.cobList.DropDownStyle = ComboBoxStyle.DropDown;//可输入下拉框 this.listData = list.ToArray(); cobList.Items.AddRange(listData);//比使用DataSource速度要快一些 cobList.TextUpdate += cobList_TextUpdate;//重新绑定事件 cobList.KeyDown += CobList_KeyDown; this.cobList.Text = defaultData; cobList.Focus(); cobList.SelectAll(); } // if (!string.IsNullOrWhiteSpace(subject)) this.Text = subject; } private void CobList_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (cobList.Items.Count == 1) cobList.Text = cobList.Items[0].ToString(); } } private void btnOK_Click(object sender, EventArgs e) { if (this.tbInputSN.Visible == true) { inputData = tbInputSN.Text; } else { if (cobList.Items.Count < 1) return; string tmp = this.cobList.Text.Trim(); var text = listData.FirstOrDefault(x => x == tmp);//忽略大小写 if (string.IsNullOrEmpty(text)) return; inputData = text; } this.DialogResult = DialogResult.OK; } private void cobList_TextUpdate(object sender, EventArgs e) { string str = cobList.Text; //获取cb_material控件输入内 //清空combobox cobList.DataSource = null; cobList.Items.Clear(); var workOrderFiltered = listData.Where(x => x.IndexOf(str, StringComparison.CurrentCultureIgnoreCase) != -1).ToArray();//忽略大小写 cobList.Items.AddRange(workOrderFiltered);//比使用DataSource速度要快一些 // 不存在符合条件时 //设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列 cobList.Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置 if (workOrderFiltered.Length > 0) { if (!cobList.DroppedDown) cobList.DroppedDown = true; // 自动弹出下拉框 } cobList.SelectionStart = str.Length; // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列 } private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } } }