从RichTextBox自动完成字典

时间:2015-11-09 14:35:23

标签: vb.net dictionary richtextbox

虽然这段代码在我的RichTextBox中运行良好,但是你如何从VB中的字典中做到呢?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'AutoComplete.Add("do")

        AutoComplete.Add("double")
        AutoComplete.Add("Apple")
        AutoComplete.Add("Car")
        AutoComplete.Add("Table")
        AutoComplete.Add("Plate")

如何从VB中的字典中执行此操作?

2 个答案:

答案 0 :(得分:4)

所以这不是一个真正的.NET字典吗?暧昧的头衔!

假设每一行都是一个单独的单词来填充您的词典':

Public Sub PopulateDict()
    For Each word As String In File.ReadAllLines("path")
        AutoComplete.Add(word)
    Next
End Sub

这样的事,是吗?

答案 1 :(得分:0)

很简单,你只需要读入字典文本文件,解析它中的单词,然后将它们添加到ArrayList中。

以下是一个例子:

Private AutoComplete As New ArrayList

Private Sub AddDictionary()
    Try
        ' Create an instance of StreamReader to read from a file.
        Using sr As StreamReader = New StreamReader("dictionary.txt")

        Dim line As String
        Dim pieces As String()

        ' Read every line in the file
        ' Split the line by the delimiter
        ' Add the individual pieces to the AutoComplete ArrayList
        Do
            line = sr.ReadLine()
            pieces = line.Split(" ")

            For Each piece In pieces
                AutoComplete.Add(piece)
            Next
        Loop Until line Is Nothing

        ' Close the dictionary file StreamReader
        sr.Close()
    End Using
        Catch E As Exception
        ' Let the user know what went wrong.
        MsgBox("The dictionary file could not be read:\n" & E.message, _
                                             MsgBoxStyle.Critical, _
                                             "Error loading file!")
    End Try
End Sub

注意:我不知道您的词典文件是如何格式化的,所以在我的示例代码中,我只是使用空格将这些行分成单个单词。

您可以通过AddDictionary()方法调用Form1_Load()子,在启动时将这些字词添加到AutoComplete字典。