0

我一直在尝试创建一个功能,允许我在 a 中写一个单词,richtextbox我将看到一个替代单词列表及其含义,或者我将能够按 abutton并用替代文本替换我写的单词.

我试过用这个Dictionary

    Public Sub wordget()

    wordDictionary = New Dictionary(Of String, String())
    wordDictionary.Add("code", {"lines", "script"})
    wordDictionary.Add("dwell", {"holding", "denn", "space"})
End Sub

嗯,然后我说dim lastword as String = richtextbox1.text.split(" ").last

然后我检查 lastword 是否等于 wordDictionary 键,如果是我则替换lastword为数组中的第一个字符串。

这种方法不是很好,因为我无法显示单词及其含义,只能显示单词。

Private Sub TextDisplay_TextChanged(sender As Object, e As EventArgs) Handles TextDisplay.TextChanged
    Dim lastword As String = TextDisplay.Text.Split(" ").Last
    Dim val As Integer

    If wordDictionary.ContainsKey(lastword) Then
        For Each wstr As String() In wordDictionary.Values
            TextDisplay.Text = TextDisplay.Text.Replace(lastword, wstr(val))
        Next
    End If
End Sub
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click

    Val = Val() + 1

End Sub

我无法让代码按照我想要的方式运行,如果我写进去 dwell,它将替换dwell为单词code。我不知道如何操作数组。

我如何使用另一种方法或这种方法来完成此操作?

4

2 回答 2

0

看起来你已经知道如何访问你的字典有点混乱。我想这就是你的意思:

[编辑:我已经用我用来测试的整个代码替换了代码片段。]

Public Class Form1
    Dim wordDictionary As Dictionary(Of String, String())

    Private Sub TextDisplay_TextChanged(sender As Object, e As EventArgs) Handles TextDisplay.TextChanged
        Dim lastword As String = TextDisplay.Text.Split(" "c).Last

        For Each kvp As KeyValuePair(Of String, String()) In wordDictionary
            If kvp.Value.Contains(lastword) Then
                TextDisplay.Text = TextDisplay.Text.Replace(lastword, kvp.Key)
                ' move the cursor to the end of the text
                TextDisplay.Select(TextDisplay.TextLength, TextDisplay.TextLength)
                ' operation has been done, exit the search
                Exit For
            End If
        Next

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        wordDictionary = New Dictionary(Of String, String())
        wordDictionary.Add("code", New String() {"lines", "script"})
        wordDictionary.Add("dwell", New String() {"holding", "denn", "space"})
    End Sub
End Class
于 2013-06-24T17:33:34.453 回答
0
    Private Sub TextDisplay_TextChanged(sender As Object, e As EventArgs) Handles TextDisplay.TextChanged
    Dim lastword As String = TextDisplay.Text.Split(" ").Last
    For Each wstr As String In wordDictionary.Keys
        If (TextDisplay.Text.Contains(wstr)) Then
            TextDisplay.Text = TextDisplay.Text.Replace(lastword, ) 'replace with the value at 0 position in the array 
        End If
    Next
End Sub
于 2013-06-24T20:08:09.047 回答