.net - 文本替换功能

标签 .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

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

我会用数据库来做这个吗?有没有人有任何例子。
谢谢你的时间。

最佳答案

在 .NET 中为您实现了所需的功能。只需执行以下操作:

1) 设置 TextBox.AutoCompleteSource属性(property)至true
2)设置TextBox.AutoCompleteMode属性(property)到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 

关于.net - 文本替换功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16763998/

相关文章:

.net - 在线程(.NET)中锁定或 try catch 字典是否更有效

c# - 语言相关部分子串的国际化 (i18n)

vb.net 使用 FindWindowex 在另一个应用程序中查找消息框

c# - 控制台应用程序中的 .NET 全局异常处理程序

vb.net - 哪个更快 - 使用 block 或 Try/Catch/Finally

.net - VS2008 on Win7 64-Bit : Debugging a Windows Service

visual-studio - SVN 自动更新

c# - 仅处理消息队列中的部分消息

.net - 如何在没有大选择/开关的情况下返回特定类类型

excel - VB.Net:在某个单元格中插入形状