我有一个问题困惑了好久没有解决,恳请大家帮帮忙
我在Richtextbox控件中输入了一段文字,比如Richtextbox1="ahfahoiqw澳网ajsiqwu我们的shfiqow城市"& vblf & "asdhqowuiey啊对哦iqwio"
现在我想是文本框中的“城市”颜色为红色。字母“a”为绿色。请问这个在vb中用程序如何实现
。因为输入的文字是不定的,字符不好确定,所以我想实现的是使文本框中,指定的字符为特定颜色
及如上所示:只要出现“城市”就显示为红色。
拜托大家,帮帮忙。谢谢啊

解决方案 »

  1.   


    '增加一个窗体,上面有二个文本框 Text1  Text2  
    '上面有一个RichTextBox1 
    '上面还有一个按扭 Command1
    '以下代码在窗体中.
    '代码功能.  当你单击按扭后,让RichText1的文本与Text1相同的显示为红色,
    '让RichText1的文本与Text2相同的显示为绿色,Option ExplicitPrivate Sub Command1_Click()
      RichTextBox1.SelStart = 0
      RichTextBox1.SelLength = Len(RichTextBox1.Text)
      RichTextBox1.SelColor = vbBlack  Dim BeginIndex As Long
      Dim TxtLen As Long
      TxtLen = Len(Text1.Text)
      BeginIndex = RichTextBox1.Find(Text1.Text, 0)   '只要文本是与Text1中的文本相同
      
      While BeginIndex > -1
         RichTextBox1.SelStart = BeginIndex
         RichTextBox1.SelLength = TxtLen
         RichTextBox1.SelColor = vbRed   '显示为红色
         
         BeginIndex = RichTextBox1.Find(Text1.Text, BeginIndex + 1)
      Wend
      
      TxtLen = Len(Text2.Text)
      BeginIndex = RichTextBox1.Find(Text2.Text, 0)   '只要文本是与Text2中的文本相同
      
      While BeginIndex > -1
         RichTextBox1.SelStart = BeginIndex
         RichTextBox1.SelLength = TxtLen
         RichTextBox1.SelColor = vbGreen   '显示为绿色
         
         BeginIndex = RichTextBox1.Find(Text1.Text, BeginIndex + 1)
      Wend
        
    End Sub