如果单击按钮,我正在创建一个为每个选定行添加特定字符的应用。例如,在Richtextbox的每一行中使用“//”并将文本着色为红色,这就像在visual studio中注释掉的功能
我试过这个,但它确实无法运作
private void toolStripButton1_Click(object sender, EventArgs e)
{
int firstCharPosition = richTextBox1.GetFirstCharIndexOfCurrentLine();
int lineNumber = richTextBox1.GetLineFromCharIndex(firstCharPosition);
int lastCharPosition = richTextBox1.GetFirstCharIndexFromLine(lineNumber + 1);
if (richTextBox1.SelectionLength > 0)
{
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectedText = "//" + richTextBox1.SelectedText.ToString();
}
else
{
richTextBox1.Select(firstCharPosition, lastCharPosition - firstCharPosition);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectedText = "//" + richTextBox1.SelectedText.ToString();
}
}
伙计们,请帮助我谢谢!
答案 0 :(得分:2)
if (richTextBox1.Text.Length > 0 && richTextBox1.SelectionLength >= 0)
{
string[] lines = richTextBox1.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
Color normalColor = Color.Black, commentColor = Color.Red;
int selStart = richTextBox1.SelectionStart, selEnd = selStart + richTextBox1.SelectionLength,
startLine = -1, endLine = -1, lineSum = 0, k = 0;
for (k = 0; k < lines.Length; k++)
if (startLine == -1)
{
if ((lineSum += lines[k].Length + 1) > selStart)
{
startLine = k;
if (selEnd <= lineSum) endLine = k;
}
}
else if (endLine == -1)
{
if ((lineSum += lines[k].Length + 1) >= selEnd)
endLine = k;
}
else break;
for (int i = 0; i < lines.Length; i++)
lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];
richTextBox1.Text = "";
richTextBox1.SelectionStart = 0;
for (int i = 0; i < lines.Length; i++)
{
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
richTextBox1.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
}
int selectStarIndx = richTextBox1.GetFirstCharIndexFromLine(startLine), selectEndIndx = richTextBox1.GetFirstCharIndexFromLine(endLine + 1);
if (selectEndIndx == -1) selectEndIndx = richTextBox1.Text.Length;
richTextBox1.Select(selectStarIndx, selectEndIndx - selectStarIndx);
richTextBox1.Focus();
}
答案 1 :(得分:1)
正确操作RichTextBox
比更改Text
更为复杂。这是一个可以帮助您的代码示例。
请注意,永远不会直接更改Text
,因此它不会搞乱以前的格式,并且会尝试恢复选择。
它允许您独立注释掉几个部分。它从TextBox
获取评论字符串进行测试,并将其恢复为Black
..
// get all line numbers that belong to the selection
List<int> getSelectedLines(RichTextBox RTB)
{
List<int> lines = new List<int>();
int sStart = RTB.SelectionStart;
int sEnd = RTB.SelectionLength + sStart;
int line1 = RTB.GetLineFromCharIndex(sStart);
int line2 = RTB.GetLineFromCharIndex(sEnd);
for (int l = line1; l <= line2; l++) lines.Add(l);
return lines;
}
// prefix a line with a string
void prependLine(RichTextBox RTB, int line, string s)
{
int sStart = RTB.SelectionStart;
int sLength = RTB.SelectionLength;
RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
RTB.SelectionLength = 0;
RTB.SelectedText = s;
RTB.SelectionStart = sStart;
RTB.SelectionLength = sLength;
}
// color one whole line
void colorLine(RichTextBox RTB, int line, Color c)
{
int sStart = RTB.SelectionStart;
int sLength = RTB.SelectionLength;
RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
RTB.SelectionLength = RTB.Lines[line].Length; ;
RTB.SelectionColor = c;
RTB.SelectionStart = sStart;
RTB.SelectionLength = sLength;
}
// additional function, may come handy..
void trimLeftLine(RichTextBox RTB, int line, int length)
{
int sStart = RTB.SelectionStart;
int sLength = RTB.SelectionLength;
RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
RTB.SelectionLength = 2;
RTB.Cut();
RTB.SelectionStart = sStart;
RTB.SelectionLength = 0;
}
// remove a string token from the start of a line
void trimLeftLine(RichTextBox RTB, int line, string token)
{
int sStart = RTB.SelectionStart;
int sLength = RTB.SelectionLength;
RTB.SelectionStart = RTB.GetFirstCharIndexFromLine(line);
RTB.SelectionLength = token.Length;
if (RTB.SelectedText == token) RTB.Cut();
RTB.SelectionStart = sStart;
RTB.SelectionLength = 0;
}
这是评论按钮:
private void button1_Click(object sender, EventArgs e)
{
List<int> lines = getSelectedLines(richTextBox1);
foreach (int l in lines) prependLine(richTextBox1, l, tb_comment.Text);
foreach (int l in lines) colorLine(richTextBox1, l, Color.Firebrick);
}
这是取消注释按钮:
private void button2_Click(object sender, EventArgs e)
{
List<int> lines = getSelectedLines(richTextBox1);
foreach (int l in lines) trimLeftLine(richTextBox1, l, tb_comment.Text);
foreach (int l in lines) colorLine(richTextBox1, l, Color.Black);
}
答案 2 :(得分:0)
这可能会完成工作
if (richTextBox1.SelectionLength >= 0 && richTextBox1.Text.Length > 0)
{
int firstCharIndex = richTextBox1.GetFirstCharIndexOfCurrentLine();
if (richTextBox1.SelectionLength == 0)
{
int lineNumber = richTextBox1.GetLineFromCharIndex(firstCharIndex);
int lastCharIndex = richTextBox1.GetFirstCharIndexFromLine(lineNumber + 1);
if (lastCharIndex == -1) lastCharIndex = richTextBox1.Text.Length;
richTextBox1.Select(firstCharIndex, lastCharIndex - firstCharIndex);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectedText = "//" + richTextBox1.SelectedText;
richTextBox1.Select(firstCharIndex--, lastCharIndex - firstCharIndex);
richTextBox1.Focus();
}
else
{
int selStart = richTextBox1.SelectionStart;
int selLen = richTextBox1.SelectionLength;
richTextBox1.SelectionStart = selStart + selLen;
int lastLineFirstChar = richTextBox1.GetFirstCharIndexOfCurrentLine();
int lastLineNumber = richTextBox1.GetLineFromCharIndex(lastLineFirstChar);
int lastLineLastChar = richTextBox1.GetFirstCharIndexFromLine(lastLineNumber + 1);
if (lastLineLastChar == -1) lastLineLastChar = richTextBox1.Text.Length;
string beforeSelection = richTextBox1.Text.Substring(0, firstCharIndex);
string afterSelection = richTextBox1.Text.Substring(lastLineLastChar, richTextBox1.Text.Length - lastLineLastChar);
string selectionText = richTextBox1.Text.Substring(firstCharIndex, lastLineLastChar - firstCharIndex);
string[] lines = selectionText.Split(new string[] { Environment.NewLine, "\r", "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string commentedText = "";
for (int i = 0; i < lines.Length; i++) commentedText += "//" + lines[i] + "\r\n";
richTextBox1.Text = beforeSelection + afterSelection;
richTextBox1.SelectionStart = firstCharIndex;
richTextBox1.SelectedText = commentedText;
richTextBox1.Select(firstCharIndex, lastLineLastChar - firstCharIndex + (lines.Length * 2));
richTextBox1.SelectionColor = Color.Red;
richTextBox1.Focus();
}
}