選択後に複数の値を取得したかったのでListをやめて、DataTableで処理。
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
using System.Data; using System.Linq; using System.Windows.Forms; namespace ProductionManagement { public partial class MasterForm : Form { public bool IsClick = false; public string ClickKey { set; get; } public string ClickValue { set; get; } public MasterForm(IColumnInfo columnInfo, DataTable dataTable) { InitializeComponent(); DataTable baseMemberTable = dataTable.DefaultView.ToTable(true, new string[] { columnInfo.PrimaryKey, columnInfo.DisplayColumn }); listBox1.DataSource = baseMemberTable; listBox1.ValueMember = columnInfo.PrimaryKey; listBox1.DisplayMember = columnInfo.DisplayColumn; textBox1.TextChanged += (s, e) => { string keyword = textBox1.Text.ToString(); if(keyword == "") { listBox1.DataSource = baseMemberTable; return; } var rowCollection = baseMemberTable.AsEnumerable() .Where(r => r[columnInfo.DisplayColumn].ToString().Contains(keyword)); if (rowCollection.Count() == 0) { listBox1.DataSource = baseMemberTable.Clone(); return; } DataTable displayMemberTable = rowCollection.CopyToDataTable(); listBox1.DataSource = displayMemberTable; }; listBox1.KeyDown += (s, e) => { if (e.KeyCode != Keys.Enter) return; ConfirmValue(); }; listBox1.KeyDown += (s, e) => { if (e.KeyCode == Keys.Escape) this.Close(); }; listBox1.DoubleClick += (s, e) => { ConfirmValue(); }; } private void ConfirmValue() { if (listBox1.SelectedValue == null) return; IsClick = true; ClickKey = listBox1.SelectedValue.ToString(); ClickValue = listBox1.Text; this.Close(); } } } |
実際にソフトを使ってもらっていると、頻繁に更新されるマスタなのでDBはちょっと面倒。正確性はそこまで必要ないけど手入力の表記ゆれは避けたいという場面が多い。
それならテキストファイルだろうということで修正してみた。
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System.IO; using System.Text; using System.Data; using System.Linq; using System.Windows.Forms; namespace ProductionManagement { public partial class MasterForm : Form { public bool IsClick = false; public string ClickedValue { set; get; } public MasterForm(string filePath) { InitializeComponent(); DataTable baseTable = new DataTable(); baseTable.Columns.Add("DisplayColumn", typeof(string)); using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("UTF-8"))) { while (sr.Peek() > -1) { var newRow = baseTable.NewRow(); newRow["DisplayColumn"] = sr.ReadLine(); baseTable.Rows.Add(newRow); } } listBox1.DataSource = baseTable; listBox1.DisplayMember = "DisplayColumn"; textBox1.TextChanged += (s, e) => UpdateValue("DisplayColumn", baseTable); KeyEventAttach(); } private void UpdateValue(string key, DataTable baseTable) { string keyword = textBox1.Text.ToString(); if (keyword == "") { listBox1.DataSource = baseTable; return; } var rowCollection = baseTable.AsEnumerable() .Where(r => r[key].ToString().Contains(keyword)); if (rowCollection.Count() == 0) { listBox1.DataSource = baseTable.Clone(); } else { listBox1.DataSource = rowCollection.CopyToDataTable(); } } public void KeyEventAttach() { listBox1.KeyDown += (s, e) => { if (e.KeyCode != Keys.Enter) return; ConfirmValue(); }; listBox1.KeyDown += (s, e) => { if (e.KeyCode == Keys.Escape) this.Close(); }; listBox1.DoubleClick += (s, e) => { ConfirmValue(); }; } private void ConfirmValue() { if (listBox1.SelectedValue == null) return; IsClick = true; ClickedValue = listBox1.Text; this.Close(); } } } |