回答編集履歴

5

追記

2019/02/21 13:32

投稿

YAmaGNZ
YAmaGNZ

スコア10242

test CHANGED
@@ -219,3 +219,43 @@
219
219
  End Class
220
220
 
221
221
  ```
222
+
223
+
224
+
225
+ 文字比較の部分について少し解説を入れておきます。
226
+
227
+ 変更前の
228
+
229
+ ```VB.NET
230
+
231
+ ComboBox1.DataSource = list.Where(
232
+
233
+ Function(s)
234
+
235
+ Return 0 = cca.Compare(Strings.Left(s, txt.Length), txt, opt)
236
+
237
+ End Function).ToArray()
238
+
239
+ ```
240
+
241
+ この部分ですが、listは`List(of String)`ですので`Function(s)`の`s`はString型となります。
242
+
243
+ ですので、比較の時は`Strings.Left(s, txt.Length)`となります。
244
+
245
+ 私の提示したソース(今回は最終の追記したソースとします)の場合は
246
+
247
+ ```VB.NET
248
+
249
+ ComboBox1.DataSource = list.Where(
250
+
251
+ Function(s)
252
+
253
+ Return 0 = cca.Compare(Strings.Left(s.ItemName, txt.Length), txt, opt)
254
+
255
+ End Function).ToArray()
256
+
257
+ ```
258
+
259
+ となっています。listは`List(of ItemInfomation)`なので`s`は`ItemInfomation型`となります。
260
+
261
+ ですので、比較の時は`Strings.Left(s.ItemName, txt.Length)`と`ItemNameプロパティ`を指定します。

4

修正

2019/02/21 13:32

投稿

YAmaGNZ
YAmaGNZ

スコア10242

test CHANGED
@@ -168,7 +168,7 @@
168
168
 
169
169
  ComboBox1.DisplayMember = "ItemName"
170
170
 
171
- ComboBox1.DataSource = New BindingSource(list, Nothing)
171
+ ComboBox1.DataSource = list
172
172
 
173
173
 
174
174
 

3

修正

2019/02/21 13:19

投稿

YAmaGNZ
YAmaGNZ

スコア10242

test CHANGED
@@ -118,32 +118,104 @@
118
118
 
119
119
  ### 追記
120
120
 
121
- 最終的な目的を考慮すると、DictionaryのValue値段にするのが簡単だと思いま
121
+ 最終的な目的を考慮すると、Dictionaryを使用するのではなくアイテム情報を格納るクラスを作成し
122
+
123
+ そのListをComboBoxのDataSourceとするのが一番すっきりするかと思います。
122
124
 
123
125
  ```VB.NET
124
126
 
127
+ Imports System.Globalization
128
+
129
+
130
+
131
+ Public Class Form1
132
+
133
+
134
+
135
+ Private list As New List(Of ItemInfomation)
136
+
137
+
138
+
125
- list.Add("あり", 10)
139
+ Private Class ItemInfomation
140
+
126
-
141
+ Public Property ItemNumber As Integer
142
+
143
+ Public Property ItemName As String
144
+
145
+ Public Property Price As Integer
146
+
147
+ End Class
148
+
149
+
150
+
151
+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
152
+
153
+
154
+
155
+ list.Add(New ItemInfomation() With {.ItemNumber = 1, .ItemName = "あり", .Price = 10})
156
+
157
+ list.Add(New ItemInfomation() With {.ItemNumber = 2, .ItemName = "ありんこ", .Price = 10})
158
+
159
+ list.Add(New ItemInfomation() With {.ItemNumber = 3, .ItemName = "いぬ(雑種)", .Price = 50000})
160
+
161
+ list.Add(New ItemInfomation() With {.ItemNumber = 4, .ItemName = "いぬ(プードル)", .Price = 200000})
162
+
163
+ list.Add(New ItemInfomation() With {.ItemNumber = 5, .ItemName = "かめ", .Price = 500})
164
+
165
+ list.Add(New ItemInfomation() With {.ItemNumber = 6, .ItemName = "かめむし", .Price = 50})
166
+
167
+
168
+
169
+ ComboBox1.DisplayMember = "ItemName"
170
+
171
+ ComboBox1.DataSource = New BindingSource(list, Nothing)
172
+
173
+
174
+
175
+ End Sub
176
+
177
+
178
+
179
+ Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
180
+
127
- list.Add("ありんこ", 10)
181
+ Dim cca = New CultureInfo("ja-jp").CompareInfo
182
+
128
-
183
+ Dim opt As CompareOptions
184
+
185
+ opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない
186
+
187
+ opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない
188
+
189
+ opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない
190
+
191
+ opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。
192
+
193
+ opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。
194
+
195
+
196
+
129
- list.Add("いぬ(雑種)", 10000)
197
+ Dim txt As String = TextBox1.Text
130
-
198
+
199
+
200
+
131
- list.Add("いぬ(ポメラニアン)", 150000)
201
+ ComboBox1.DataSource = list.Where(
202
+
132
-
203
+ Function(s)
204
+
205
+ Return 0 = cca.Compare(Strings.Left(s.ItemName, txt.Length), txt, opt)
206
+
133
- list.Add("かめ", 500)
207
+ End Function).ToArray()
208
+
209
+ End Sub
210
+
211
+
212
+
213
+ Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
214
+
215
+ TextBox2.Text = DirectCast(ComboBox1.SelectedValue, ItemInfomation).Price
216
+
217
+ End Sub
218
+
219
+ End Class
134
220
 
135
221
  ```
136
-
137
- そうすれば、
138
-
139
- ```VB.NET
140
-
141
- Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
142
-
143
- TextBox2.Text = ComboBox1.SelectedValue.ToString
144
-
145
- End Sub
146
-
147
- ```
148
-
149
- こうするだけで、値段がテキストボックスへ表示されます。

2

追記

2019/02/21 13:15

投稿

YAmaGNZ
YAmaGNZ

スコア10242

test CHANGED
@@ -113,3 +113,37 @@
113
113
 
114
114
 
115
115
  ```
116
+
117
+
118
+
119
+ ### 追記
120
+
121
+ 最終的な目的を考慮すると、DictionaryのValueを値段にするのが簡単だと思います。
122
+
123
+ ```VB.NET
124
+
125
+ list.Add("あり", 10)
126
+
127
+ list.Add("ありんこ", 10)
128
+
129
+ list.Add("いぬ(雑種)", 10000)
130
+
131
+ list.Add("いぬ(ポメラニアン)", 150000)
132
+
133
+ list.Add("かめ", 500)
134
+
135
+ ```
136
+
137
+ そうすれば、
138
+
139
+ ```VB.NET
140
+
141
+ Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
142
+
143
+ TextBox2.Text = ComboBox1.SelectedValue.ToString
144
+
145
+ End Sub
146
+
147
+ ```
148
+
149
+ こうするだけで、値段がテキストボックスへ表示されます。

1

追記

2019/02/21 12:57

投稿

YAmaGNZ
YAmaGNZ

スコア10242

test CHANGED
@@ -31,3 +31,85 @@
31
31
 
32
32
 
33
33
  あとはWhereを少し変えれば改変前と同じ動作をするかと思います。
34
+
35
+
36
+
37
+ サンプル追記
38
+
39
+ ```VB.NET
40
+
41
+ Imports System.Globalization
42
+
43
+
44
+
45
+ Public Class Form1
46
+
47
+
48
+
49
+ Private list As New Dictionary(Of String, Integer)
50
+
51
+
52
+
53
+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
54
+
55
+
56
+
57
+ list.Add("あり", 1)
58
+
59
+ list.Add("ありんこ", 2)
60
+
61
+ list.Add("いぬ", 1)
62
+
63
+ list.Add("いぬねこ", 2)
64
+
65
+
66
+
67
+ ComboBox1.DisplayMember = "key"
68
+
69
+ ComboBox1.ValueMember = "value"
70
+
71
+ ComboBox1.DataSource = New BindingSource(list, Nothing)
72
+
73
+
74
+
75
+ End Sub
76
+
77
+
78
+
79
+ Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
80
+
81
+ Dim cca = New CultureInfo("ja-jp").CompareInfo
82
+
83
+ Dim opt As CompareOptions
84
+
85
+ opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない
86
+
87
+ opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない
88
+
89
+ opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない
90
+
91
+ opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。
92
+
93
+ opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。
94
+
95
+
96
+
97
+ Dim txt As String = TextBox1.Text
98
+
99
+
100
+
101
+ ComboBox1.DataSource = New BindingSource(list.Where(
102
+
103
+ Function(s)
104
+
105
+ Return 0 = cca.Compare(Strings.Left(s.Key, txt.Length), txt, opt)
106
+
107
+ End Function).ToArray(), Nothing)
108
+
109
+ End Sub
110
+
111
+ End Class
112
+
113
+
114
+
115
+ ```