0

我正在尝试创建一个函数,该函数将允许用户将文本输入到 RTB 中,如果该文本作为键存在于字典中,则 alistboxvalues与它们相关的所有对象填充,每个都dictionary填充新的线。keyvaluelistbox

第一行突出显示,用户可以按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

结束类

4

1 回答 1

1

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

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
于 2013-06-30T01:48:57.670 回答