如何从字典值填充ListBox



我正试图创建一个函数,允许用户将文本输入RTB,如果该文本作为关键字存在于字典中,则listbox由与其相关的keydictionary的所有values填充,每个value在新行中填充listbox

第1行被高亮显示并且用户可以按下CCD_ 7并且用高亮显示的文本替换RTB中的文本。

我是VB的新手,所以我知道的不多。

这就是我目前所拥有的。

Public Class Oxnay
Private Sub Oxnay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Tsort()
End Sub
Private TDictionary As Dictionary(Of String, String())
Public Sub Tsort()
    TDictionary = New Dictionary(Of String, String())
    TDictionary.Add("ape", {"pl", "tz", "xu"})
    TDictionary.Add("lor", {"tv", "px"})
End Sub
Private Sub RichtextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    Dim lastword As String = RichTextBox1.Text.Split(" ").Last
    If RichTextBox1.ContainsKey(lastword) Then
        'display each string of the dictionary array related to lastword in different lines
        'highlight first line
        'Some[Code]
    Else
        ListBox1.Text = ""
    End If
End Sub

最终类

对于第一个"查找"部分,请尝试以下操作:

Private Sub RichtextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
    Dim lastword As String = RichTextBox1.Text.Trim.Split(" ").Last
    ListBox1.Items.Clear()
    If Not IsNothing(TDictionary) AndAlso TDictionary.ContainsKey(lastword) Then
        ListBox1.Items.AddRange(TDictionary(lastword))
    End If
End Sub

然后用列表框中的选择替换当前选择的文本:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    If ListBox1.SelectedIndex <> -1 Then
        If RichTextBox1.SelectedText <> "" Then
            RichTextBox1.SelectedText = ListBox1.SelectedItem.ToString
        End If
    End If
End Sub

最新更新