user2423014
user2423014

Reputation:

Text Replacement Function

I'm working with visual basic. How do i create a function to read from a list of words while typing and replace any word with the possible completed word while writing . like a t9 text function . this is the code I am working with .

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

this method may be useful to some persons and i hope you will like it but for me it is a very bad method to use because i would have to enter thousands of words manually .

would I do this with a database? and does anyone have any examples . Thanks you for your time .

Upvotes: 2

Views: 436

Answers (2)

VladL
VladL

Reputation: 13043

The needed functionality is implemented for you in .NET. Just do following:

1) set the TextBox.AutoCompleteSource Property to true

2) set the TextBox.AutoCompleteMode property to Suggest

3) load the the word list from the file (you will find enough on the web) and set it to TextBox.AutoCompleteCustomSource Property similar to this:

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

textbox1.AutoCompleteCustomSource = MySource 

Upvotes: 2

FraserOfSmeg
FraserOfSmeg

Reputation: 1148

I think your best bet for this would be a text file that is loaded into memory when the application starts up. I would imagine you'd want to have a listbox created at runtime at the location of the textboxs current carrot locations (plus some x and y so the textbox is clearly visible above/below the listbox) and then you could have all possible options in the listbox for the user to click the correct answer. Is this the kind of thing you are looking for?

Here's a link to a dictionary text file you could use, although it would need some processing to only contain words:

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

Upvotes: 1

Related Questions