简体   繁体   中英

RichTextBox inserting text

For Windows Forms.

I am trying to insert text into the .rtf field of a RichTextBox.

I have tried two methods. When I use .Rtf.insert, nothing happens at all.

When I edit the .rtf string based on the selected text positions myself, I either end up adding gibberish to the thing or getting an error that says that the file format is invalid. My best guess is that this is because the .rtf string is in .rtf format and the selection index that I am using is based on the plain text string and so I am inserting the text in the wrong location in the .rtf string and messing up the RTF code.

But knowing what the problem is (if I am correct) hasn't helped me solve it.

Is there a way to get .rtf.insert to work correctly, or is there a way to translate the selected text indexes to the actual .rtf text positions so that something like the code below would work? I am assuming that the RichTextBox itself must know how to translate the one index into another because it can insert characters when the user types just fine.

Here is my code snippet. The point of the code is to insert a marker into the text that will later be parsed and replaced with a student's first name. There will be other such codes. "codeLeader" and "codeEnder" are just the strings I use to surround the codes with. In this case I am using "[*" and *]" to indicate that there is a code I will need to parse, but I put them into separate strings so that I can easily change it if I wish. I have actually already written the parsing code, which works just fine on rich text. It is just inserting the text into the richTextBox itself that is the problem. In other words, if I were to type the codes by hand it would work just fine. But this would be troublesome for the user because some of the codes will use index numbers.

private void studentFirstNameCode_Click(object sender, EventArgs e)
{
    string ins = f1ref.codeLeader;
    ins += "SNFirst" + f1ref.codeEnder;
    int start = editorField.richTextBox1.SelectionStart;
    if (start == -1) { start = 0; }
    int end = start + editorField.richTextBox1.SelectionLength;
    if (end == -1) { end = 0; }
    string pre = editorField.richTextBox1.Rtf.Substring(0, start);
    string post = editorField.richTextBox1.Rtf.Substring(end);
    string newstring = pre + ins + post;
    editorField.richTextBox1.Rtf = newstring;
    // this also doesn't work.  gives no result at all.
    // editorField.richTextBox1.Rtf.Insert(start, newstring);
}

I don't think that you need to use the RTF property to simple insert a text inside the RichTextBox actual text. In particular because you don't seem to add an RTF formatted text.

If you don't want to use RTF then the simplest way to accomplish your goal is just one line of code

editorField.SelectedText = yourParameterText;

This will work as you have pasted the text from the clipboard in the selected position (eventually replacing text if something is selected) and the base work of correctly formatting your text inside the RTF is done by the control itself

I have found a work-around by using .SendKeys. This makes the text appear a bit slowly (as if typed very quickly) so isn't optimal, but it does work.

It is enough for a workable solution, but I am still troubled by the problem. It seems like this issue should have a more elegant solution than this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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