How do I prepend text to a selected line in a Rich Text Box?
Let's say I have some batch code that prints out a hello world program in batch, and I have placed the code into a rich text box (that is in visual C# .NET):
@echo off
cls
title Hello World
echo Hello World
pause
Now, I want to be able t开发者_如何学Goo highlight a line, or part of a line, and then comment out that highlighted line by adding a "::" to the front.
So for example, if I highlighted "echo", and then clicked a button, it would insert a "::" to the front of the line.
How would I do that in code?
int selStart = myRichTextBox.SelectionStart;
int selLength = myRichTextBox.SelectionLength;
int line = myRichTextBox.GetLineFromCharIndex(selStart);
int endLine = myRichTextBox.GetLineFromCharIndex(selStart + selLength);
for(; line <= endLine; line++) {
int charIndex = myRichTextBox.GetFirstCharIndexFromLine(line);
myRichTextBox.Select(charIndex, 0);
myRichTextBox.SelectedText = "::";
}
myRichTextBox.Select(selStart, selLength);
Is about how I would do it. It will handle multiple lines, and it will add the comment on the selected lines regardless of the actual selection area.
精彩评论