簡體   English   中英

如何從字典值填充ListBox?

[英]How to populate ListBox from Dictionary Values?

我正在嘗試創建一個函數,該函數將允許用戶將文本輸入到實時出價工具中,如果該文本作為“鍵”存在於“字典”中,則listbox將填充與它們相關的keydictionary的所有values ,每個value用新行填充listbox

第一行突出顯示,用戶可以按下enter button並用突出顯示的文本替換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

然后將當前選擇的文本替換為ListBox中的選擇:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM