質問するログイン新規登録

回答編集履歴

5

追記

2019/02/21 13:32

投稿

YAmaGNZ
YAmaGNZ

スコア10702

answer CHANGED
@@ -108,4 +108,24 @@
108
108
  TextBox2.Text = DirectCast(ComboBox1.SelectedValue, ItemInfomation).Price
109
109
  End Sub
110
110
  End Class
111
- ```
111
+ ```
112
+
113
+ 文字比較の部分について少し解説を入れておきます。
114
+ 変更前の
115
+ ```VB.NET
116
+ ComboBox1.DataSource = list.Where(
117
+ Function(s)
118
+ Return 0 = cca.Compare(Strings.Left(s, txt.Length), txt, opt)
119
+ End Function).ToArray()
120
+ ```
121
+ この部分ですが、listは`List(of String)`ですので`Function(s)`の`s`はString型となります。
122
+ ですので、比較の時は`Strings.Left(s, txt.Length)`となります。
123
+ 私の提示したソース(今回は最終の追記したソースとします)の場合は
124
+ ```VB.NET
125
+ ComboBox1.DataSource = list.Where(
126
+ Function(s)
127
+ Return 0 = cca.Compare(Strings.Left(s.ItemName, txt.Length), txt, opt)
128
+ End Function).ToArray()
129
+ ```
130
+ となっています。listは`List(of ItemInfomation)`なので`s`は`ItemInfomation型`となります。
131
+ ですので、比較の時は`Strings.Left(s.ItemName, txt.Length)`と`ItemNameプロパティ`を指定します。

4

修正

2019/02/21 13:32

投稿

YAmaGNZ
YAmaGNZ

スコア10702

answer CHANGED
@@ -83,7 +83,7 @@
83
83
  list.Add(New ItemInfomation() With {.ItemNumber = 6, .ItemName = "かめむし", .Price = 50})
84
84
 
85
85
  ComboBox1.DisplayMember = "ItemName"
86
- ComboBox1.DataSource = New BindingSource(list, Nothing)
86
+ ComboBox1.DataSource = list
87
87
 
88
88
  End Sub
89
89
 

3

修正

2019/02/21 13:19

投稿

YAmaGNZ
YAmaGNZ

スコア10702

answer CHANGED
@@ -58,18 +58,54 @@
58
58
  ```
59
59
 
60
60
  ### 追記
61
- 最終的な目的を考慮すると、DictionaryのValue値段にするのが簡単だと思いま
61
+ 最終的な目的を考慮すると、Dictionaryを使用するのではなくアイテム情報を格納るクラスを作成し
62
+ そのListをComboBoxのDataSourceとするのが一番すっきりするかと思います。
62
63
  ```VB.NET
64
+ Imports System.Globalization
65
+
66
+ Public Class Form1
67
+
68
+ Private list As New List(Of ItemInfomation)
69
+
63
- list.Add("あり", 10)
70
+ Private Class ItemInfomation
71
+ Public Property ItemNumber As Integer
72
+ Public Property ItemName As String
73
+ Public Property Price As Integer
74
+ End Class
75
+
76
+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
77
+
78
+ list.Add(New ItemInfomation() With {.ItemNumber = 1, .ItemName = "あり", .Price = 10})
79
+ list.Add(New ItemInfomation() With {.ItemNumber = 2, .ItemName = "ありんこ", .Price = 10})
80
+ list.Add(New ItemInfomation() With {.ItemNumber = 3, .ItemName = "いぬ(雑種)", .Price = 50000})
81
+ list.Add(New ItemInfomation() With {.ItemNumber = 4, .ItemName = "いぬ(プードル)", .Price = 200000})
82
+ list.Add(New ItemInfomation() With {.ItemNumber = 5, .ItemName = "かめ", .Price = 500})
83
+ list.Add(New ItemInfomation() With {.ItemNumber = 6, .ItemName = "かめむし", .Price = 50})
84
+
85
+ ComboBox1.DisplayMember = "ItemName"
86
+ ComboBox1.DataSource = New BindingSource(list, Nothing)
87
+
88
+ End Sub
89
+
90
+ Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
64
- list.Add("ありんこ", 10)
91
+ Dim cca = New CultureInfo("ja-jp").CompareInfo
92
+ Dim opt As CompareOptions
93
+ opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない
94
+ opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない
95
+ opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない
96
+ opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。
97
+ opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。
98
+
65
- list.Add("いぬ(雑種)", 10000)
99
+ Dim txt As String = TextBox1.Text
100
+
66
- list.Add("いぬ(ポメラニアン)", 150000)
101
+ ComboBox1.DataSource = list.Where(
102
+ Function(s)
103
+ Return 0 = cca.Compare(Strings.Left(s.ItemName, txt.Length), txt, opt)
67
- list.Add("かめ", 500)
104
+ End Function).ToArray()
68
- ```
69
- そうすれば、
105
+ End Sub
70
- ```VB.NET
106
+
71
107
  Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
72
- TextBox2.Text = ComboBox1.SelectedValue.ToString
108
+ TextBox2.Text = DirectCast(ComboBox1.SelectedValue, ItemInfomation).Price
73
109
  End Sub
110
+ End Class
74
- ```
111
+ ```
75
- こうするだけで、値段がテキストボックスへ表示されます。

2

追記

2019/02/21 13:15

投稿

YAmaGNZ
YAmaGNZ

スコア10702

answer CHANGED
@@ -55,4 +55,21 @@
55
55
  End Sub
56
56
  End Class
57
57
 
58
- ```
58
+ ```
59
+
60
+ ### 追記
61
+ 最終的な目的を考慮すると、DictionaryのValueを値段にするのが簡単だと思います。
62
+ ```VB.NET
63
+ list.Add("あり", 10)
64
+ list.Add("ありんこ", 10)
65
+ list.Add("いぬ(雑種)", 10000)
66
+ list.Add("いぬ(ポメラニアン)", 150000)
67
+ list.Add("かめ", 500)
68
+ ```
69
+ そうすれば、
70
+ ```VB.NET
71
+ Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
72
+ TextBox2.Text = ComboBox1.SelectedValue.ToString
73
+ End Sub
74
+ ```
75
+ こうするだけで、値段がテキストボックスへ表示されます。

1

追記

2019/02/21 12:57

投稿

YAmaGNZ
YAmaGNZ

スコア10702

answer CHANGED
@@ -14,4 +14,45 @@
14
14
  ```
15
15
  このように設定します。
16
16
 
17
- あとはWhereを少し変えれば改変前と同じ動作をするかと思います。
17
+ あとはWhereを少し変えれば改変前と同じ動作をするかと思います。
18
+
19
+ サンプル追記
20
+ ```VB.NET
21
+ Imports System.Globalization
22
+
23
+ Public Class Form1
24
+
25
+ Private list As New Dictionary(Of String, Integer)
26
+
27
+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
28
+
29
+ list.Add("あり", 1)
30
+ list.Add("ありんこ", 2)
31
+ list.Add("いぬ", 1)
32
+ list.Add("いぬねこ", 2)
33
+
34
+ ComboBox1.DisplayMember = "key"
35
+ ComboBox1.ValueMember = "value"
36
+ ComboBox1.DataSource = New BindingSource(list, Nothing)
37
+
38
+ End Sub
39
+
40
+ Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
41
+ Dim cca = New CultureInfo("ja-jp").CompareInfo
42
+ Dim opt As CompareOptions
43
+ opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない
44
+ opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない
45
+ opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない
46
+ opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。
47
+ opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。
48
+
49
+ Dim txt As String = TextBox1.Text
50
+
51
+ ComboBox1.DataSource = New BindingSource(list.Where(
52
+ Function(s)
53
+ Return 0 = cca.Compare(Strings.Left(s.Key, txt.Length), txt, opt)
54
+ End Function).ToArray(), Nothing)
55
+ End Sub
56
+ End Class
57
+
58
+ ```