如何在Richtextbox的每一行的开头添加一个字符
how to add a character at the beginning of each line in Richtextbox
我正在开发一个应用程序,如果您单击该按钮,它会为选定的每一行添加一个特定的字符。例如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();
}
}
大家帮帮我谢谢!
这可能会完成工作
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();
}
}
正确操作 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 = length;
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);
}
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();
}
我正在开发一个应用程序,如果您单击该按钮,它会为选定的每一行添加一个特定的字符。例如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();
}
}
大家帮帮我谢谢!
这可能会完成工作
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();
}
}
正确操作 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 = length;
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);
}
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();
}