已知一个字符串,写在richbox中,如何查找里边有相同的IP地址都用红色表示,
例如:
202.108.22.5,192.168.1.1,127.0.0.1,192.168.1.1
如上,出现过2次以上(包括两次)的IP都用红色表示?

解决方案 »

  1.   

    Private Sub Command1_Click()
    Dim FoundPos As Integer
       Dim FoundLine As Integer
      Dim a()
     s = "192.168.1.1"
     l = Len(Trim(s))
     Do While sd < Len(RichTextBox1.Text) - l
     sd = sd + 1
       FoundPos = RichTextBox1.Find(s, sd, sd + l + 1, rtfWholeWord)
       
    If FoundPos <> -1 Then
    ReDim Preserve a(k)
    a(k) = FoundPos
     k = k + 1
     End If
       Loop
      For I = 0 To UBound(a)
      RichTextBox1.SelStart = a(I)
      RichTextBox1.SelLength = l
      RichTextBox1.SelColor = vbRed
    Next
    End SubPrivate Sub Form_Load()
    RichTextBox1.Text = "202.108.22.5,192.168.1.1,127.0.0.1,192.168.1.1,202.108.22.5"
    End Sub
      

  2.   

    简化一下:
    Private Sub Command1_Click()
    Dim FoundPos As Integer
       Dim FoundLine As Integer
     s = "192.168.1.1"
     l = Len(Trim(s))
     Do While sd < Len(RichTextBox1.Text) - l
     sd = sd + 1
       FoundPos = RichTextBox1.Find(s, sd, sd + l + 1, rtfWholeWord)
       
    If FoundPos <> -1 Then
    RichTextBox1.SelStart = FoundPos
      RichTextBox1.SelLength = l
      RichTextBox1.SelColor = vbRed
     End If
       Loop
     End Sub
    Private Sub Form_Load()
    RichTextBox1.Text = "202.108.22.5,192.168.1.1,127.0.0.1,192.168.1.1,202.108.22.5"
    End Sub
      

  3.   

    Dim aa$, keyword$
    Private Sub Form_Activate()
        Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
        aa = "c:\tt.txt"
        RichTextBox1.LoadFile aa, 1
        keyword = UCase("192.168.1.1")
    End SubPrivate Sub Command1_Click()
        Dim jj&
        aa = UCase(Trim(RichTextBox1.Text))
        jj = 0
        Do
           DoEvents
           jj = InStr(jj + 1, aa, keyword)
           If jj = 0 Then Exit Do
           RichTextBox1.SelStart = jj
           RichTextBox1.SelLength = Len(keyword)
           RichTextBox1.SelColor = QBColor(12)     '选中字体变红色
        Loop
    End Sub