(文字列、文字列の) 辞書があり、辞書の値を使用してディレクトリ パスを設定し、キーを使用して各値の説明を設定します。
例:
Value of a random item of my dictionary: "C:\Test"
Key name of that item = "Test folder"
さて、私のアプリケーションでは、説明を表示するか、完全なディレクトリ パスを表示するかを選択するオプションがあり、ここに問題があります...
それを理解するためのいくつかの画像:
1 - ここで、辞書のアイテムを保存/管理します。
2 - このチェックボックスを使用して、辞書のキーまたは辞書の値をコンボボックスに表示できます (次の画像で確認できます)。
3 - 説明チェックボックスがチェックされている場合、説明がコンボボックスに表示され、項目を追加すると、選択したコンボボックステキスト (説明またはフルパス) が追加されます。
4 - ここで、説明のチェックボックスをオフにします。コンボボックスの説明を同等のフルパスに変更したのと同じ方法で、リストビューの説明を変更したいのです (ただし、コンボボックスの名前を変更するために行うことは、リロードすることです)。辞書はアルファベット順にソートされているため...リストビューのサブアイテムで同じことを行う方法がわかりません)
リストビューのサブアイテム テキストは、説明またはフル パスである可能性があることを覚えておいてください。そのため、この変更を両方の方法で検討する必要があります。
...そしてここに私のコードがあります:
(コメントを読んでください)
' First of all
' "Item.SubItems(2).Text" can be the description or it can be the full path, so I need to do it with both alternatives
If ListView_Monitor.Items.Count <> 0 Then
For Each Item As ListViewItem In ListView_Monitor.Items
If ShowDescriptions Then ' Showdescription is a boolean var to show descriptions or full paths
' Description is stored in the "Dictionary.Key"
' I don't know how to get the key name of the item
' Item.SubItems(2).Text = Directories_SendTo.keys ... ...
' CType(Item.SubItems(2).Text, Directories_SendTo... ...)
ElseIf Not ShowDescriptions Then ' Don't show descriptions, I will show fullpaths
' Fullpath is stored in the "Dictionary.Value"
' Remember that "Item.SubItems(2).Text" can be the description or the fullpath
' So if "Item.SubItems(2).Text" is the description then this piece of code works, 'cause the dictionary keyname is the same as the description name
Item.SubItems(2).Text = Directories_SendTo(Item.SubItems(2).Text)
' Here I need an alternative if "Item.SubItems(2).Text" is the directory path and not the description
End If
Next
End If
アップデート:
解決策(今のところ)...
私の質問は、このコードを改善できるかどうかです(おそらくdict内でループしないようにするためです):
If ListView_Monitor.Items.Count <> 0 Then
For Each Item As ListViewItem In ListView_Monitor.Items
If ShowDescriptions Then ' Show descriptions
For Each value In Directories_SendTo.Values
If value = Item.SubItems(2).Text Then
Item.SubItems(2).Text = FindKeyByValue(Directories_SendTo, Item.SubItems(2).Text)
End If
Next
ElseIf Not ShowDescriptions Then ' Show fullpaths
For Each key In Directories_SendTo.Keys
If key = Item.SubItems(2).Text Then
Item.SubItems(2).Text = Directories_SendTo(key)
End If
Next
End If
Next
End If
Public Function FindKeyByValue(Of TKey, TValue)(dictionary As Dictionary(Of TKey, TValue), value As TValue) As TKey
For Each pair As KeyValuePair(Of TKey, TValue) In dictionary
If value.Equals(pair.Value) Then Return pair.Key
Next
' Throw New Exception("The value is not found in the dictionary.")
Return Nothing
End Function