文本框格式

时间:2009-07-22 20:08:15

标签: .net

是否存在基于内容格式化文本框(或richtextbox)内容的库,假设内容符合模式(实质上是语法高亮)?如果这在网络世界和winform中都是可能的,那将是很好的,尽管我更喜欢winform(或WPF)。

3 个答案:

答案 0 :(得分:1)

您需要做的就是以编程方式选择文本,然后设置SelectionColor属性。当然,您需要编写正则表达式来确定要选择的文本,但之后将其着色很简单。

是啊,是的这不适用于TextBox,只适用于RichTextBox(显然)。

答案 1 :(得分:1)

您可以自己在富文本框中执行此操作,只需选择文本并设置颜色即可。

然而,那里有更多精心设计的图书馆......

例如,由于您提到了WinForms,您可能需要查看SyntaxEditor by ActiPro

答案 2 :(得分: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