質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Visual Basic .NET

Microsoft Visual Basic .NET (VB.NET)とはオブジェクト志向のプログラム言語です。 Microsoft"s Visual Basic 6 のバージョンアップとしてみることができますが、Microsoft.NET Frameworktによって動かされています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

Q&A

解決済

1回答

2061閲覧

テキストボックスに文字を検索とselectindexを変更したい

退会済みユーザー

退会済みユーザー

総合スコア0

Visual Basic .NET

Microsoft Visual Basic .NET (VB.NET)とはオブジェクト志向のプログラム言語です。 Microsoft"s Visual Basic 6 のバージョンアップとしてみることができますが、Microsoft.NET Frameworktによって動かされています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Visual Studio 2013

Microsoft Visual Studio 2013は、Microsoftによる統合開発環境(IDE)であり、多種多様なプログラミング言語に対応しています。 Visual Studio 2012の次のバージョンです

0グッド

1クリップ

投稿2019/02/20 11:34

編集2019/02/21 15:03

###実践、やりたい事と変更したい場所
変更前のコード

Imports System.Globalization Public Class Form1 Private list As New List(Of String)() Private Sub Kaou_Load(sender As Object, e As EventArgs) Handles MyBase.Load list.Add("あり") list.Add("ありんこ") list.Add("いぬ") list.Add("いのしし") list.Add("うさぎ") end sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedIndex = 0 Then TextBox2.Text = "10" End If If ComboBox1.SelectedIndex = 1 Then TextBox2.Text = "10" End If If ComboBox1.SelectedIndex = 2 Then end if If ComboBox1.SelectedIndex = 3 Then TextBox2.Text = "100000" End If If ComboBox1.SelectedIndex = 4 Then TextBox2.Text = "150000" End If end sub '値段は例です全部で5個あります Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged 'テキストを書き換えるたびに、 'リストの内容を先頭一致でフィルタリング Dim ci = New CultureInfo("ja-jp").CompareInfo Dim opt As CompareOptions opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。 opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。 Dim txt As String = TextBox3.Text ComboBox1.DataSource = list.Where( Function(s) Return 0 = ci.Compare(Strings.Left(s, txt.Length), txt, opt) End Function).ToArray() End Sub End Class

変更前のコードは全体のコードです。
・「変更前」で実践
textbox2=値段、textbox3=文字入力するとその文字が表示される
あ=あり、ありんこ(カタカナでも可能)
い=いぬ、いのしし
う=うさぎ・・・など他の文字は表示されません。


・indexとtextbox2これでやってます。
この場合新しいアイテム追加(間に入れるときも)して書き出し
追加前
index="1"Then’ありんこ
textbox2.text=""
追加後
index="2"Then'ありんこ
textbox2.text=""
こんな感じで何個があります。
これを書き直しなど大変なのでindexとtextbox破棄しました。

変更後のコード

Imports System.Globalization Public Class Form1 Private list As New Dictionary(Of String, Integer) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load      ’変更前の list.Add("あり") '変更後のアイテム list.Add("あり", 1) list.Add("ありんこ", 2) list.Add("いぬ", 1) list.Add("いぬねこ", 2) ComboBox1.DisplayMember = "key" ComboBox1.ValueMember = "value" ComboBox1.DataSource = New BindingSource(list, Nothing) ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = ComboBox1.SelectedValue.ToString End Sub Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged Dim cca = New CultureInfo("ja-jp").CompareInfo Dim opt As CompareOptions opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。 opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。 Dim txt As String = TextBox2.Text ComboBox1.DataSource = New BindingSource(list.Where( Function(s) Return 0 = cca.Compare(Strings.Left(s.Key, txt.Length), txt, opt) End Function).ToArray(), Nothing) End Sub End Class

全体でのコードです。
###変更後のやりたい事と修正したい
変更後のコードで文字をいれても変わりません
**値段の方は

TextBox1.Text = ComboBox1.SelectedValue.ToString

解決?しました。
変更後
textbox1が値段、textbox2を文字を入力(検索)にしました。
###わからない事、教えてほしいこと
文字を入力後、comboboxのアイテムが消える
「あ」を入れてもあり、ありんこのみ表示しない
変更前はあをいれるとあり、ありんことほかの文字は出ません。
このように実行させたいのですがご迷惑をおかけしすいません。
・文字を入力後の改善修正を教えてもらえますでしょうか?
申し訳ございません。変更前と変更後の書き直ししました。
いろいろと試しにコード変えてみましたが4,5時間やってもできませんでした。

初心者ですがよろしくお願いいたします。
###バージョン
Windows10
Vistual basic
Vistual studio2019を使ってます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

SelectedIndexイベントでやりたいこと(*は追加して番号を書き換えする(数字を入れ直し)のがとても大変)というのがイマイチ分かりませんが

変更前と同じようにするのであれば、DictionaryEntryではなく、Dictionary(Of TKey, TValue)を使うべきです。

何故DictionaryEntryではダメかと言うと、IEnumerableを実装していないため、Whereが使えません。
ですので、IEnumerableを実装しているDictionary(Of TKey, TValue)のほうを使うべきです。

改変前は事前に項目のリストを作成しているのに対し、改変後はComboBoxへ直接Addしています。
これも改変前と同じように事前にDictionaryを作成するようにしてください。

また、ComboBoxのDataSourceへDictionaryを設定する場合ですが

VB.NET

1ComboBox1.DataSource = New BindingSource(list, Nothing)

このように設定します。

あとはWhereを少し変えれば改変前と同じ動作をするかと思います。

サンプル追記

VB.NET

1Imports System.Globalization 2 3Public Class Form1 4 5 Private list As New Dictionary(Of String, Integer) 6 7 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 8 9 list.Add("あり", 1) 10 list.Add("ありんこ", 2) 11 list.Add("いぬ", 1) 12 list.Add("いぬねこ", 2) 13 14 ComboBox1.DisplayMember = "key" 15 ComboBox1.ValueMember = "value" 16 ComboBox1.DataSource = New BindingSource(list, Nothing) 17 18 End Sub 19 20 Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 21 Dim cca = New CultureInfo("ja-jp").CompareInfo 22 Dim opt As CompareOptions 23 opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない 24 opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない 25 opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない 26 opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。 27 opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。 28 29 Dim txt As String = TextBox1.Text 30 31 ComboBox1.DataSource = New BindingSource(list.Where( 32 Function(s) 33 Return 0 = cca.Compare(Strings.Left(s.Key, txt.Length), txt, opt) 34 End Function).ToArray(), Nothing) 35 End Sub 36End Class 37

追記

最終的な目的を考慮すると、Dictionaryを使用するのではなくアイテム情報を格納するクラスを作成し
そのListをComboBoxのDataSourceとするのが一番すっきりするかと思います。

VB.NET

1Imports System.Globalization 2 3Public Class Form1 4 5 Private list As New List(Of ItemInfomation) 6 7 Private Class ItemInfomation 8 Public Property ItemNumber As Integer 9 Public Property ItemName As String 10 Public Property Price As Integer 11 End Class 12 13 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 14 15 list.Add(New ItemInfomation() With {.ItemNumber = 1, .ItemName = "あり", .Price = 10}) 16 list.Add(New ItemInfomation() With {.ItemNumber = 2, .ItemName = "ありんこ", .Price = 10}) 17 list.Add(New ItemInfomation() With {.ItemNumber = 3, .ItemName = "いぬ(雑種)", .Price = 50000}) 18 list.Add(New ItemInfomation() With {.ItemNumber = 4, .ItemName = "いぬ(プードル)", .Price = 200000}) 19 list.Add(New ItemInfomation() With {.ItemNumber = 5, .ItemName = "かめ", .Price = 500}) 20 list.Add(New ItemInfomation() With {.ItemNumber = 6, .ItemName = "かめむし", .Price = 50}) 21 22 ComboBox1.DisplayMember = "ItemName" 23 ComboBox1.DataSource = list 24 25 End Sub 26 27 Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 28 Dim cca = New CultureInfo("ja-jp").CompareInfo 29 Dim opt As CompareOptions 30 opt = opt Or CompareOptions.IgnoreWidth '全角と半角を区別しない 31 opt = opt Or CompareOptions.IgnoreKanaType 'ひらがなとカタカナを区別しない 32 opt = opt Or CompareOptions.IgnoreCase '大文字と小文字を区別しない 33 opt = opt Or CompareOptions.IgnoreNonSpace '文字列比較で分音文字などの結合の分音文字を無視することを示します。 34 opt = opt Or CompareOptions.IgnoreSymbols '文字列の比較が空白文字が区切り記号、通貨記号、パーセント記号、数学記号、アンパサンド、やなどの記号を無視することを示します。 35 36 Dim txt As String = TextBox1.Text 37 38 ComboBox1.DataSource = list.Where( 39 Function(s) 40 Return 0 = cca.Compare(Strings.Left(s.ItemName, txt.Length), txt, opt) 41 End Function).ToArray() 42 End Sub 43 44 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged 45 TextBox2.Text = DirectCast(ComboBox1.SelectedValue, ItemInfomation).Price 46 End Sub 47End Class

文字比較の部分について少し解説を入れておきます。
変更前の

VB.NET

1ComboBox1.DataSource = list.Where( 2 Function(s) 3 Return 0 = cca.Compare(Strings.Left(s, txt.Length), txt, opt) 4 End Function).ToArray()

この部分ですが、listはList(of String)ですのでFunction(s)sはString型となります。
ですので、比較の時はStrings.Left(s, txt.Length)となります。
私の提示したソース(今回は最終の追記したソースとします)の場合は

VB.NET

1ComboBox1.DataSource = list.Where( 2 Function(s) 3 Return 0 = cca.Compare(Strings.Left(s.ItemName, txt.Length), txt, opt) 4 End Function).ToArray()

となっています。listはList(of ItemInfomation)なのでsItemInfomation型となります。
ですので、比較の時はStrings.Left(s.ItemName, txt.Length)ItemNameプロパティを指定します。

投稿2019/02/20 12:23

編集2019/02/21 13:32
YAmaGNZ

総合スコア10222

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/02/21 05:34 編集

YAmaGNZさん 回答ありがとうございます。 教えて頂いたコードを入れましたら textbox2のコード ComboBox1.DataSource = New BindingSource(list, Nothing) list.Where( 実行したら文字を入力すると消えます。 Dictionary(Of TKey, TValue)も確認したんですがC#たっだので C#はよくわからないです(本当にすいません) 後、foam‗roadに ComboBox1.DataSource = New BindingSource(list, Nothing) を追加したら、コンボボックスのアイテムが空白になってました。
YAmaGNZ

2019/02/20 21:35

>変更前のコードだと >index=0then >textbox3.text="1" すみません。さっぱり分かりません。textbox3には最終的に何を表示したいのですか? また、コードに関してはサンプルを追記します。
退会済みユーザー

退会済みユーザー

2019/02/21 06:07

サンプル参考になりました。ありがとうございました。 すいません、字に誤りありました。 変更前で作ったコード 前のコード(字誤り修正します) Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged If ComboBox1.SelectedIndex = 0 Then TextBox2.Text = "1" '省略 Index=0 Then~index=49 Then(50個分の動物) End If >何を表示させたい事は 「動物の値段」を表示です。 50個のアイテムがlist.Add("動物")50個あり、その間に追加した時 indexの番号も51個になるので間に追加したのをindex="?" とtextbox2.text=""'値段 同じ名前を間にlist.add("")追加したいので index="" Then textbox2.text="" indexとtextbox2も間に番号と値段も書き直しすることになってしまいました。
退会済みユーザー

退会済みユーザー

2019/02/21 06:49

>変更前のコード if combobox1.selectIndex="0"Then'1~50個 textbox3.text=""'値段を表示する end sub アイテムが50個の動物を追加しました。 その時にアイテムを間list.Add("") (動物の名前の順にいれたい)に追加した時 indexとtextbox3も間に入れたアイテムの番号と値段を書き直しすることに なりした。 変更後のコードでindexとtextboxは破棄しました。 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim de As New DictionaryEntry Dim cb1 As New Integer de = CType(ComboBox1.SelectedItem, DictionaryEntry) cb1 = de.Value TextBox1.Text = (cb1).ToString() End Sub textbox1に値段を表示させます。textbox2に文字を検索 最終的に文字をテキストボックスに入れると「あ」をいれると あの付いた動物でいうとコンボボックスのアイテムで アリ、アサリ、アナゴなどそれ以外(いがつく文字)は表示しない 変更前はそれで出来ました すいません。もう一個いいでしょうか?
退会済みユーザー

退会済みユーザー

2019/02/21 06:55

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged Dim de As New DictionaryEntry Dim cb1 As New Integer de = CType(ComboBox1.SelectedItem, DictionaryEntry) cb1 = de.Value TextBox1.Text = (cb1).ToString() End Sub この「de = CType(ComboBox1.SelectedItem, DictionaryEntry)」 で実行した時 指定されたキャストは有効ではありませんが出てしまいました。 対策ありますか?
YAmaGNZ

2019/02/21 12:48

キャストに関しては、私が提示したソースのままであるのであれば、DictionaryEntryではなくDictionaryにキャストすべきです。 また、最終的な目的の為の手段を回答に追記しておきます。
YAmaGNZ

2019/02/21 13:03

できれば断片的なソースではなく、こう変えましたの全体ソースとそれによって発生している問題をまとめて記述してください。 変更前と変更後(どこからの変更かも分かりません)が入り乱れて最終的な問題点がいまいち把握出来ていません。
YAmaGNZ

2019/02/21 22:08

変更後のソースを確認しました。 Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged ここが間違っており、TextBox1のイベントハンドラとなっています。 Handles TextBox1.TextChanged の部分を修正したら動作します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問