C# 如何在Richtextbox中的每行开头添加字符
我正在做一个应用程序,如果你点击按钮,它会为每一行添加一个特定的字符。例如,在Richtextbox中的每一行中输入“/”,并将文本涂成红色,这类似于VisualStudio中的注释输出功能 我试过了,但不管用C# 如何在Richtextbox中的每行开头添加字符,c#,winforms,richtextbox,C#,Winforms,Richtextbox,我正在做一个应用程序,如果你点击按钮,它会为每一行添加一个特定的字符。例如,在Richtextbox中的每一行中输入“/”,并将文本涂成红色,这类似于VisualStudio中的注释输出功能 我试过了,但不管用 private void toolStripButton1_Click(object sender, EventArgs e) { int firstCharPosition = richTextBox1.GetFirstCharIndexOfCurrentLin
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();
}
}
if(richTextBox1.SelectionLength>=0&&richTextBox1.Text.Length>0)
{
int firstCharIndex=richTextBox1.GetFirstCharIndexOfCurrentLine();
如果(richTextBox1.SelectionLength==0)
{
int lineNumber=richTextBox1.GetLineFromCharIndex(firstCharIndex);
int lastCharIndex=richTextBox1.GetFirstCharIndexFromLine(行号+1);
如果(lastCharIndex==-1)lastCharIndex=richTextBox1.Text.Length;
richTextBox1.选择(firstCharIndex,lastCharIndex-firstCharIndex);
richTextBox1.SelectionColor=Color.Red;
richTextBox1.SelectedText=“/”+richTextBox1.SelectedText;
richTextBox1.选择(firstCharIndex--,lastCharIndex-firstCharIndex);
richTextBox1.Focus();
}
其他的
{
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);
如果(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(新字符串[]{Environment.NewLine,“\r”,“\n”,“\r\n”},StringSplitOptions.RemoveEmptyEntries);
字符串commentedText=“”;
对于(int i=0;i
正确操作RichTextBox
比更改文本要复杂一些。下面是一个代码示例,它将帮助您
请注意,is从不直接更改文本
,因此它不会弄乱以前的格式,并尝试恢复选择
它允许您独立地注释几个部分。它从文本框
中获取注释字符串进行测试,并恢复为黑色
// 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;
}
//获取属于所选内容的所有行号
列出getSelectedLines(RichTextBox RTB)
{
列表行=新列表();
int ssstart=RTB.SelectionStart;
int sEnd=RTB.SelectionLength+ssStart;
int line1=RTB.GetLineFromCharIndex(sStart);
int line2=RTB.GetLineFromCharIndex(发送);
对于(int l=line1;lif(richTextBox1.Text.Length>0&&richTextBox1.SelectionLength>=0)
{
string[]lines=richTextBox1.Text.Split(新字符串[]{Environment.NewLine,“\n”,“\r”,“\r\n”},StringSplitOptions.RemoveEmptyEntries);
颜色normalColor=Color.Black,commentColor=Color.Red;
int selStart=richTextBox1.SelectionStart,selEnd=selStart+richTextBox1.SelectionLength,
STARTINE=-1,endLine=-1,lineSum=0,k=0;
对于(k=0;kselStart)
{
startine=k;
如果(selEnd=selEnd)
终点线=k;
}
否则就断了;
对于(int i=0;i 行[i]=(i>=startLine&&i不客气。但是当我测试时,出现了一个问题。如果你不在乎,请跳过。否则我还有一个解决方案。为此,你必须在richTextBox中写10或12行。一起选择第3、4、5行。通过单击事件对其进行注释。它变为红色。之后,选择第6、7、8行并对其进行注释。它变为红色,但上面的行返回黑色。告诉我您是否需要解决方案hh我现在看到问题..如何解决此问题,先生?以及如果我有新按钮,如何取消注释已注释文本?感谢回答我sirhi先生,我发现了一个问题先生..我已经写了10行,我选择1和2并对其进行注释,在我选择2和4后,它变为红色,并显示rns红色所有10行我会看一看,并将从StringSplitOptions.RemoveEmptyEntries发布到StringSplitOptions.Nonesir现在工作正常,我如何在另一个按钮中取消注释注释注释行..请帮助我,先生,谢谢作为一个新问题提问。我会回答
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();
}