2009-07-22 69 views
0

是否有庫會提供基於內容格式化文本框(或richtextbox)的內容,假定內容符合模式(本質上是語法突出顯示)?這將是很好,如果這可能在網絡世界以及winform,但我更喜歡winform(或WPF)。文本框格式化

+0

可能的重複:http://stackoverflow.com/questions/1087735/a-textbox-richtextbox-that-has-syntax-highlighting-c – 2009-07-22 20:11:39

回答

1

所有你需要做的就是以編程方式選擇文本,然後設置SelectionColor屬性。當然,您需要編寫能夠確定要選擇的文本的正則表達式,但之後着色卻很簡單。

噢耶;這不適用於TextBox,只適用於RichTextBox(顯然)。

1

您可以自己在一個富文本框中通過選擇文本並設置顏色來完成此操作。

不過,也有更復雜的圖書館有...

例如,既然你提到的WinForms,你可能想看看SyntaxEditor by ActiPro

1

這是你需要的一點。 它將選擇第一個到第十個字符 或選擇RichTextBox的全長 然後更改選擇的顏色。 關鍵是,一旦你做出選擇,你正在對選擇進行更改,而不是整個RichTextBox。 然後你可以改變字體爲粗體。 粗體更加粗糙。

'select the first character 
rtbRichTextBox.SelectionStart = 0 
'Select the length forward as far as you need to 
rtbRichTextBox.SelectionLength = 10 'Len(rtbRichTextBox.Text) 

' change the text color 
rtbRichTextBox.SelectionColor = Color.Blue 

' make a highlight color over the text 
'rtbRichTextBox.SelectionBackColor = Color.Yellow 

Dim newFontStyle As System.Drawing.FontStyle 

If rtbRichTextBox.SelectionFont IsNot Nothing Then 
    newFontStyle = FontStyle.Bold 
    rtbRichTextBox.SelectionFont = New Font(MyObj_Font_Arial.FontFamily, _ 
              MyObj_Font_Arial.Size, _ 
              newFontStyle) 
end if 

'a more straight forward bold would be to change the font. 
Dim MyObjectArialFont As New Font("Arial", 6.5, FontStyle.Bold) 
rtbRichTextBox.SelectionFont = MyObjectArialFont