简体   繁体   English

如何在C#中操作RichTextBox?

[英]How do I manipulate a RichTextBox in C#?

Say, I have two RichTextBox. 说,我有两个RichTextBox。 I will paste a code snippet on at first one with format, indentation and Syntax highlight. 我将首先在代码片段上粘贴格式,缩进和语法高亮显示。

I want to add only code tag before starting point and last end of the text. 我想在开始文本的最后一端和最后一端之前添加代码标记。 When I will hit okay button, it will show it in the next RichTextBox with the tag and everything of the code snippet will be as it is. 当我点击okay按钮时,它会在带有标签的下一个RichTextBox中显示它,并且代码片段的所有内容都将保持原样。

Start out with this: 从这开始:

rtext2.Text = "<code>" + rtext1.Text + "</code>";

Does that give you an idea of what you're trying to do? 这会让你知道你想要做什么吗?

I think this code might work for you if you are using Windows Forms application 我认为如果您使用的是Windows窗体应用程序,此代码可能对您有用

    //Give the RichTextBox some text.
    string sometext = "www.asp.net.";
    rchSource.Text = sometext;
    rchSource.Select(sometext.IndexOf("www"), "www".Length);
    rchSource.SelectionFont = new Font(rchSource.SelectionFont, FontStyle.Italic);

    rchSource.Select(sometext.IndexOf("."), ".".Length);
    rchSource.SelectionFont = new Font(rchSource.SelectionFont, FontStyle.Bold);
    rchSource.SelectionColor = Color.Brown;

    rchSource.Select(sometext.IndexOf("asp"), "asp".Length);
    rchSource.SelectionFont = new Font(rchSource.SelectionFont, FontStyle.Bold);
    rchSource.SelectionColor = Color.Red;

    rchSource.Select(sometext.IndexOf("net"), "net".Length);
    rchSource.SelectionFont = new Font(rchSource.SelectionFont, FontStyle.Underline);

    rchSource.Select(0, 0);

Reference link: java2s.com 参考链接: java2s.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM