2017-06-17 110 views
0

我在VB.NET 2010上进行了实验,并试图更改特定文本的颜色。更改标题的颜色RichTextBox VB.NET

我创建了拍摄的当前窗口标题,并在RichTextBox中实现了它的定时器:

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles activetitle.Tick 
    If strin <> GetActiveWindowTitle() Then  
     RichTextBox1.Text = RichTextBox1.Text & vbNewLine & vbNewLine & "[---" & GetActiveWindowTitle() & "---]") & vbNewLine & vbNewLine 
     strin = GetActiveWindowTitle() 
    End If 
End Sub 

不过,我试图改变代码,以便在当前的活动标题保存在RichTextBox的,它会出现在不同的颜色:

 Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles activetitle.Tick 
    If strin <> GetActiveWindowTitle() Then 
     RichTextBox1.Text = RichTextBox1.Text & vbNewLine & vbNewLine & "[---" & GetActiveWindowTitle() & "---]" & vbNewLine & vbNewLine 
     strin = GetActiveWindowTitle() 
    End If 
    If RichTextBox1.Text.Contains("[---" & "---]") Then 
     RichTextBox1.SelectionColor = Color.Red 
    End If 
End Sub 

我尝试许多不同的变化,并期待所有在互联网上为这个但没有成功:(

有谁知道我可以如何改变实现到RichTextBox中的当前活动窗口标题的颜色?

我想提醒你,我不希望在RichTextBox的颜色全变,但只有当前活动窗口标题:)

感谢的颜色提前所有评论!

+0

你设置'SelectionColor'财产,但计时器逻辑似乎被忘记在更改颜色之前设置“SelectionStart”和“SelectionLength”。 – Icepickle

+0

那么我应该如何改变这个代码呢? –

+0

找到想要着色的文本位于文本框内,并将该位置设置为SelectionStart,然后将SelectionLength属性的长度设置为想要着色的文本的长度,然后应用SelectionColor – Icepickle

回答

0

所以,我认为你正在自己动手了很多困难比你应该,你可以重写像下面

Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles activetitle.Tick 
    If strin <> GetActiveWindowTitle() Then 
     Dim oldColor = RichTextBox1.SelectionColor 
     ' save the title first 
     strin = GetActiveWindowTitle() 
     ' set the cursor at the end (to be sure) 
     RichTextBox1.SelectionStart = RichTextBox1.Length 
     ' check if the text is empty 
     If String.IsNullOrWhiteSpace(strin) Then 
      ' if yes, set the color to red 
      RichTextBox1.SelectionColor = Color.Red 
     End If 
     ' add the new text to the RichTextBox 
     RichTextBox1.AppendText(Environment.NewLine & "[---" & strin & "---]" & Environment.NewLine) 
     ' revert to the previous selectioncolor 
     RichTextBox1.SelectionColor = oldColor 
    End If 
End Sub 
+0

Ty用于回复更容易,但它不成功:(还有其他的东西它被添加到我的文本框,所以它不是纯粹的复制到它的标题我试图你的代码,但这是我得到gyazo.com/e80a95f31eb08de460428b795fb40168 –

+0

@EricCartman我只能看到你现在有东西在红色,我认为你的意图是我从你的问题中可以看到的,因此我不确定你的问题仍然存在,你能否更新你有其他信息的问题? – Icepickle

+0

对不起,不清楚 - 图像显示,我的登录标题中只有一个是红色的,但我希望所有人都被涂成红色。但我不希望我的键盘日志记录为红色(如果您看到,我说“记事本”)。总体而言:除我写的内容外,一切都是红色 –