2009-07-22 75 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