如何在VB中创建高级文本替换数据库

时间:2013-06-24 16:46:51

标签: database vb.net visual-studio dictionary replace

我一直在尝试创建一个允许我在richtextbox中写一个单词的函数,我会看到一个替代单词列表及其含义,或者我可以按{{1用替代文本替换我写的单词。

我尝试过使用button

Dictionary
然后我说 Public Sub wordget() wordDictionary = New Dictionary(Of String, String()) wordDictionary.Add("code", {"lines", "script"}) wordDictionary.Add("dwell", {"holding", "denn", "space"}) End Sub

然后我检查lastword是否等于wordDictionary键,如果是i然后用数组中的第一个字符串替换dim lastword as String = richtextbox1.text.split(" ").last

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

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。我不知道如何操纵数组。

如何使用其他方法或此方法完成此操作?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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