文本替换功能

时间:2013-05-26 21:49:02

标签: .net vb.net visual-studio text

我正在使用visual basic。 如何创建一个函数,在键入时从单词列表中读取,并在写入时用可能完成的单词替换任何单词。像t9文本功能。 这是我正在使用的代码。

Public Class Keyboard2
Private Property dval As Integer

Private Sub GoToNext_Click(sender As Object, e As EventArgs) Handles GoToNext.Click
    'when this button is pressed the next possible word will be genereated and will replace the previous word by calling the "GetWord" Sub
    GetWord()
End Sub


Private Sub GetWord()
    dval = dval + 1 ' this value is used to  ensure that there can be no error in word replacement and it separates each change. 
    Dim lastWord As String = RichTextBox1.Text.Split(" ").Last ' get the last word entered in the text box
    If dval = 1 AndAlso RichTextBox1.Text.EndsWith("top") AndAlso lastWord = "top" Then
        'To change the last word to the next possible word
        RichTextBox1.Text = String.Concat(RichTextBox1.Text.Remove(RichTextBox1.Text.Length - lastWord.Length), "topmost")
    End If
    If dval = 2 AndAlso RichTextBox1.Text.EndsWith("topmost") AndAlso lastWord = "topmost" Then
        RichTextBox1.Text = String.Concat(RichTextBox1.Text.Remove(RichTextBox1.Text.Length - lastWord.Length), "topping")
    End If
    If dval = 3 AndAlso RichTextBox1.Text.EndsWith("topping") AndAlso lastWord = "topping" Then
        RichTextBox1.Text = String.Concat(RichTextBox1.Text.Remove(RichTextBox1.Text.Length - lastWord.Length), "top")
        dval = 0
    End If
End Sub

End Class

这种方法可能对某些人有用,我希望你会喜欢它,但对我来说这是一个非常糟糕的使用方法,因为我必须手动输入数千个单词。

我会用数据库吗?并且有没有人有任何例子。 谢谢你的时间。

2 个答案:

答案 0 :(得分:2)

在.NET中为您实现所需的功能。只需做以下事情:

1)将TextBox.AutoCompleteSource属性设置为true

2)将TextBox.AutoCompleteMode属性设置为Suggest

3)从文件中加载单词列表(您将在网上找到足够的单词列表)并将其设置为TextBox.AutoCompleteCustomSource与此类似的属性:

    Dim MySource As New AutoCompleteStringCollection()
MySource.AddRange(New String() _
                    { _
                        "January", _
                        "February", _
                        "March", _
                        "April", _
                        "May", _
                        "June", _
                        "July", _
                        "August", _
                        "September", _
                        "October", _
                        "November", _
                        "December" _
                    })

textbox1.AutoCompleteCustomSource = MySource 

答案 1 :(得分:1)

我认为你最好的选择是在应用程序启动时加载到内存中的文本文件。我想你想要在运行时在文本框当前胡萝卜位置的位置创建一个列表框(加上一些x和y,这样文本框在列表框的上方/下方清晰可见)然后你可以拥有所有可能的选项用户单击正确答案的列表框。这是你要找的东西吗?

这是您可以使用的字典文本文件的链接,虽然它需要一些处理才能包含单词:

http://www.gutenberg.org/files/29765/29765-8.txt

相关问题