如何删除Richtextbox中每行开头的一个字符
How to remove a character at the beginning of each line in Richtextbox
我正在创建一个应用程序,如果您单击该按钮,它将删除每个选定行的特定字符。
例如:我在一个RichTextbox
中每一行都加了“//”,然后把文字涂成红色,就像在Visual Studio.[=12中注释和取消注释的功能一样=]
问题是如何删除每一行中的“//”和return默认颜色的颜色?
此代码用于添加“//”并将其着色为红色:
private void toolStripButton1_Click(object sender, EventArgs e)
{
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();
}
}
请帮助我如何使用新按钮取消对注释行的注释。
private void commentOrUnComment(RichTextBox rtb, bool isUnComment)
{
if (rtb.Text.Length > 0 && rtb.SelectionLength >= 0)
{
string[] lines = rtb.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
Color normalColor = Color.Black, commentColor = Color.Red;
int selStart = rtb.SelectionStart, selEnd = selStart + rtb.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++)
if (isUnComment)
lines[i] = (i >= startLine && i <= endLine ? (lines[i].TrimStart().StartsWith("//") ? lines[i].Substring(0, lines[i].IndexOf("//"))
+ lines[i].Substring(lines[i].IndexOf("//") + 2) : lines[i]) : lines[i]);
else
lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];
rtb.Text = "";
rtb.SelectionStart = 0;
for (int i = 0; i < lines.Length; i++)
{
rtb.SelectionStart = rtb.Text.Length;
rtb.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
rtb.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
}
int selectStarIndx = rtb.GetFirstCharIndexFromLine(startLine), selectEndIndx = rtb.GetFirstCharIndexFromLine(endLine + 1);
if (selectEndIndx == -1) selectEndIndx = rtb.Text.Length;
rtb.Select(selectStarIndx, selectEndIndx - selectStarIndx);
rtb.Focus();
}
}
private void btnComment_Click(object sender, EventArgs e)
{
commentOrUnComment(richTextBox1, false);
}
private void btnUncomment_Click(object sender, EventArgs e)
{
commentOrUnComment(richTextBox1, true);
}
我正在创建一个应用程序,如果您单击该按钮,它将删除每个选定行的特定字符。
例如:我在一个RichTextbox
中每一行都加了“//”,然后把文字涂成红色,就像在Visual Studio.[=12中注释和取消注释的功能一样=]
问题是如何删除每一行中的“//”和return默认颜色的颜色?
此代码用于添加“//”并将其着色为红色:
private void toolStripButton1_Click(object sender, EventArgs e)
{
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();
}
}
请帮助我如何使用新按钮取消对注释行的注释。
private void commentOrUnComment(RichTextBox rtb, bool isUnComment)
{
if (rtb.Text.Length > 0 && rtb.SelectionLength >= 0)
{
string[] lines = rtb.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
Color normalColor = Color.Black, commentColor = Color.Red;
int selStart = rtb.SelectionStart, selEnd = selStart + rtb.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++)
if (isUnComment)
lines[i] = (i >= startLine && i <= endLine ? (lines[i].TrimStart().StartsWith("//") ? lines[i].Substring(0, lines[i].IndexOf("//"))
+ lines[i].Substring(lines[i].IndexOf("//") + 2) : lines[i]) : lines[i]);
else
lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];
rtb.Text = "";
rtb.SelectionStart = 0;
for (int i = 0; i < lines.Length; i++)
{
rtb.SelectionStart = rtb.Text.Length;
rtb.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
rtb.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
}
int selectStarIndx = rtb.GetFirstCharIndexFromLine(startLine), selectEndIndx = rtb.GetFirstCharIndexFromLine(endLine + 1);
if (selectEndIndx == -1) selectEndIndx = rtb.Text.Length;
rtb.Select(selectStarIndx, selectEndIndx - selectStarIndx);
rtb.Focus();
}
}
private void btnComment_Click(object sender, EventArgs e)
{
commentOrUnComment(richTextBox1, false);
}
private void btnUncomment_Click(object sender, EventArgs e)
{
commentOrUnComment(richTextBox1, true);
}