【问题标题】:Find and color all occurences of a String in a RichTextBox在 RichTextBox 中查找所有出现的字符串并为其着色
【发布时间】:2013-09-09 10:08:52
【问题描述】:

我正在使用 RichTextBox 控件,

我想做一个接收字符串、颜色和RichTextBox(obj)的函数,然后该函数将为函数接收的字符串的所有实例着色。

我尝试创建此功能,但结果不佳。

我的功能头是:

Public Sub ColorWord(ByVal T As RichTextBox, ByVal Word As String, ByVal color1 As Color)

例如:

            RichTextBox1.text = xxxxxtextxxxxxtextxxx

            After Runing the function like this :
            ColorWord(RichTextBox1,"text",Color.red)

结果应该是:

xxxxx文本xxxxx文本xxx

【问题讨论】:

    标签: vb.net


    【解决方案1】:
    static void Highlight(RichTextBox box, string word , Color color) {
      int pos =0;
      string s = box.Text;
      for (int i = 0; ; ) {
        int j = s.IndexOf(word , i, StringComparison.CurrentCultureIgnoreCase);
        if (j < 0) break;
        box.SelectionStart = j;
        box.SelectionLength = word .Length;
        box.SelectionColor = color;
        i = j + 1;
      }
      box.SelectionStart = pos;
      box.SelectionLength = 0;
    }
    

    将代码转换为 VB.net:

    Public Sub hl(ByVal T As RichTextBox, ByVal Word As String, ByVal color1 As Color)
           Dim pos As Integer = 0
           Dim s As String = T.Text
           Dim i As Integer = 0
           Dim StopWhile As Boolean = False
           While Not StopWhile
               Dim j As Integer = s.IndexOf(Word, i)
               If j < 0 Then
                   StopWhile = True
               Else
                   T.Select(j, Word.Length)
                   T.SelectionColor = color1
                   i = j + 1
               End If
           End While
           T.Select(pos, 0)
       End Sub
    

    【讨论】:

      【解决方案2】:
      Public Enum Highlight_Type
          Forecolor
          Backcolor
      End Enum
      Public Sub Reset_Highlight(ByVal T As RichTextBox, ByVal HighlightColorReset As Color, ByVal Type As Highlight_Type)
          On Error Resume Next
          T.SelectAll()
          Select Case Type
              Case Highlight_Type.Backcolor
                  T.SelectionBackColor = HighlightColorReset
              Case Highlight_Type.Forecolor
                  T.SelectionColor = HighlightColorReset
          End Select
          T.HideSelection = True
      End Sub
      Public Sub HighLight(ByVal T As RichTextBox, ByVal Word As String, ByVal HighlightColor As Color, ByVal HighlightColorReset As Color, ByVal Type As Highlight_Type)
          On Error Resume Next
          Reset_Highlight(T, HighlightColorReset, Type)
          If Word = Nothing Then Exit Sub
          Dim pos As Integer = 0
          Dim s As String = T.Text
          Dim i As Integer = 0
          Dim StopWhile As Boolean = False
          While Not StopWhile
              Dim j As Integer = s.IndexOf(Word, i)
              If j < 0 Then
                  StopWhile = True
              Else
                  T.Select(j, Word.Length)
                  Select Case Type
                      Case Highlight_Type.Backcolor
                          T.SelectionBackColor = HighlightColor
                      Case Highlight_Type.Forecolor
                          T.SelectionColor = HighlightColor
                  End Select
                  i = j + 1
              End If
          End While
          T.Select(pos, 0)
      End Sub
      

      【讨论】:

      • 怎么样更好?你的代码更复杂,在回答一个 +1 岁的问题时,你甚至没有试图解释它有什么更好的地方。
      • 您可能想阅读How Do I Write A Good Answer。我删除了“广告”——OP 决定哪种解决方案最适合他,我敢怀疑默认的 on error resume next 会受到专业人士的称赞。
      猜你喜欢
      • 2015-01-26
      • 2011-04-20
      • 2012-10-12
      • 1970-01-01
      • 2010-12-27
      • 1970-01-01
      相关资源
      最近更新 更多