2013-10-17 46 views
0

我想要一个VB程序,该程序将允许我将单词放入VB的文本框中(最好在关闭程序时保存这些单词以便下次出现)这些单词与s分离的可能性更大,以允许多个单词,例如“饥饿”,“查看”,“文章”用于突出显示单词文档中的单词的程序

这是第一部分,第二部分是我希望程序读取任何打开的单词文档中的文本以及文本框中的那些单词也将被突出显示。如果它不能这样做,它可以读取任何打开的文档,那么是否有可能使它能够附加一个文字文件供它阅读?

我想要一个程序,你可以写多个单词,然后让这些单词在任何单词文档中突出显示。

+0

用这个例子来创建文档... HTTP://support.microsoft.com/kb/316383使用..这个例子todo word doc中的各种格式... http://social.msdn.microsoft.com/Forums/en-US/403d73a8-b9e4-4e67-976b-971ceaeb4533/vbnet-and-word-formatting-text ?forum = vbinterop – foxtrotZulu

+0

用这个打开word文档... http://social.msdn.microsoft.com/Forums/vstudio/en-US/7ea68bac-dbaf-4989-8eef-7d3586b2faa5/opening-word-and -excel-files-from-vbnet – foxtrotZulu

+0

至于第一部分,创建一个stringbuilder或数组,添加包含新字的现有单词,将逗号分隔的字符串或枚举数组保存到xml文件中。当应用程序再次加载时,解析XML,加载字段。 – foxtrotZulu

回答

0

我已经写了这段代码,它会教你如何使用正则表达式来查找和突出显示单词/短语,你可以很容易地将它适应于WORD文档阅读,那么问题的最重要部分将是这种解决:这里还

#Region " [RichTextBox] FindNext RegEx " 

' [ FindNext RegEx ] 
' 
' //By Elektro [email protected] 
' 
' Examples : 
' 
' RichTextBox1.Text = "Hello World!, Hello World!, Hello World!" 
' 
' FindNext(RichTextBox1, "hello", FindDirection.Down, System.Text.RegularExpressions.RegexOptions.IgnoreCase, Color.LightBlue, Color.Black) 
' FindNext(RichTextBox1, "hello", FindDirection.Up, System.Text.RegularExpressions.RegexOptions.IgnoreCase, Color.Red, Color.Black) 
' 
' Private Sub RichTextBox_Enter(sender As Object, e As EventArgs) ' Handles RichTextBox1.Enter 
' ' Restore Selection Colors before search next match. 
' sender.SelectionBackColor = DefaultBackColor 
' sender.SelectionColor = DefaultForeColor 
' End Sub 

Public Enum FindDirection As Short 
    Up = 0 
    Down = 1 
End Enum 

' FindNext 
Private Sub FindNext(ByVal [Control] As RichTextBox, _ 
          ByVal SearchText As String, _ 
          ByVal Direction As FindDirection, _ 
          Optional ByVal IgnoreCase As System.Text.RegularExpressions.RegexOptions = System.Text.RegularExpressions.RegexOptions.None, _ 
          Optional ByVal Highlight_BackColor As Color = Nothing, _ 
          Optional ByVal Highlight_ForeColor As Color = Nothing) 

    If [Control].TextLength = 0 Then Exit Sub 

    ' Start searching at 'SelectionStart'. 
    Dim Search_StartIndex As Integer = [Control].SelectionStart 

    ' Stores the MatchIndex count 
    Dim matchIndex As Integer = 0 

    ' Flag to check if it's first find call 
    Static First_Find As Boolean = True 

    ' Checks to don't ommit the selection of first match if match index is exactly at 0 start point. 
    If First_Find _ 
     AndAlso Search_StartIndex = 0 _ 
     AndAlso Direction = FindDirection.Down Then 
     Search_StartIndex = -1 
     First_Find = False 
    ElseIf Not First_Find _ 
     AndAlso Search_StartIndex = 0 _ 
     AndAlso Direction = FindDirection.Down Then 
     First_Find = False 
     Search_StartIndex = 0 
    End If 

    ' Store the matches 
    Dim matches As System.Text.RegularExpressions.MatchCollection = _ 
     System.Text.RegularExpressions.Regex.Matches([Control].Text, _ 
                SearchText, _ 
                IgnoreCase Or If(Direction = FindDirection.Up, _ 
                     System.Text.RegularExpressions.RegexOptions.RightToLeft, _ 
                     System.Text.RegularExpressions.RegexOptions.None)) 

    If matches.Count = 0 Then First_Find = True : Exit Sub 

    ' Restore Highlight colors of previous selection 
    [Control].SelectionBackColor = [Control].BackColor 
    [Control].SelectionColor = [Control].ForeColor 

    ' Set next selection Highlight colors 
    If Highlight_BackColor = Nothing Then Highlight_BackColor = [Control].BackColor 
    If Highlight_ForeColor = Nothing Then Highlight_ForeColor = [Control].ForeColor 

    ' Set the match selection 
    For Each match As System.Text.RegularExpressions.Match In matches 

     matchIndex += 1 

     Select Case Direction 

      Case FindDirection.Down 
       If match.Index > Search_StartIndex Then ' Select next match 
        [Control].Select(match.Index, match.Length) 
        Exit For 
       ElseIf match.Index <= Search_StartIndex _ 
       AndAlso matchIndex = matches.Count Then ' Select first match 
        [Control].Select(matches.Item(0).Index, matches.Item(0).Length) 
        Exit For 
       End If 

      Case FindDirection.Up 
       If match.Index < Search_StartIndex Then ' Select previous match 
        [Control].Select(match.Index, match.Length) 
        Exit For 
       ElseIf match.Index >= Search_StartIndex _ 
       AndAlso matchIndex = matches.Count Then ' Select last match 
        [Control].Select(matches.Item(0).Index, matches.Item(0).Length) 
        Exit For 
       End If 

     End Select 

    Next match 

    ' Set the current selection BackColor 
    [Control].SelectionBackColor = Highlight_BackColor 
    ' Set the current selection ForeColor 
    [Control].SelectionColor = Highlight_ForeColor 
    ' Scroll to Caret/Cursor selection position 
    [Control].ScrollToCaret() 

End Sub 

#End Region 

你可以看到一个视频笔画演示:http://www.youtube.com/watch?v=mWRMdlC5DH8