コンボボックス単体で処理しようとしたけど、DroppedDownをいじるとマウスが動かなくなるので、とりあえずテキストボックス、リストボックスで対応。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System.Data; using System.Linq; using System.Collections.Generic; using System.Windows.Forms; namespace ProductionManagement { public partial class MasterDialog : Form { private string _TableKey = "name"; private List<string> _BaseMemberList = new List<string>(); private List<string> _DisplayMemberList = new List<string>(); private DataTable CreateDataTable(List<string> memberList) { DataTable dt = new DataTable(); dt.Columns.Add(_TableKey); foreach (string m in memberList) { dt.Rows.Add((dt.NewRow()[_TableKey] = m)); } return dt; } public MasterDialog() { InitializeComponent(); _BaseMemberList = new List<string> { "abc", "123", "0000", "999", "xyz","123123", "a", "abc", "123", "0000", "999", "xyz","123123", "a" }; listBox1.DataSource = CreateDataTable(_BaseMemberList); listBox1.DisplayMember = _TableKey; listBox1.ValueMember = _TableKey; textBox1.TextChanged += (s, e) => { string keyword = ((TextBox)s).Text.ToString(); if(keyword == "") { listBox1.DataSource = CreateDataTable(_BaseMemberList); } else { _DisplayMemberList = _BaseMemberList.Where(x => x.Contains(keyword)).ToList(); listBox1.DataSource = CreateDataTable(_DisplayMemberList); } }; } } } |