C# 连接线
在windows窗体上,我有几行RichTextBox和一些文本。还有一个按钮 我喜欢当我点击那个按钮时,将所有richtextbox的行连接在一行中,但不喜欢松散的文本样式(如字体系列、颜色等) 我无法使用Replace(如\r\n)执行此操作,也无法使用Replace(Environment.NewLine,“”)执行此操作。。。。。。。。 :-(( 我也尝试替换\par和\pard,但仍然没有运气 请帮忙C# 连接线,c#,richtextbox,paragraph,C#,Richtextbox,Paragraph,在windows窗体上,我有几行RichTextBox和一些文本。还有一个按钮 我喜欢当我点击那个按钮时,将所有richtextbox的行连接在一行中,但不喜欢松散的文本样式(如字体系列、颜色等) 我无法使用Replace(如\r\n)执行此操作,也无法使用Replace(Environment.NewLine,“”)执行此操作。。。。。。。。 :-(( 我也尝试替换\par和\pard,但仍然没有运气 请帮忙 这一个是不好的,因为与松散的字体定义(颜色,粗体,取消线等) 好的,再一次更具体地
这一个是不好的,因为与松散的字体定义(颜色,粗体,取消线等) 好的,再一次更具体地说 我有RichTextBox控件,有4行文本:
line 1
line 2
line 3
line 4
第3行是红色的
我需要得到以下信息:
line 1 line 2 line 3 line 4
(而且“第3行”应该像以前一样是红色的)
当我尝试
richTextBox1.Text=richTextBox1.Text.Replace(Environment.NewLine,“”)
…我得到:
line 1
line 2
line 34
“第2行”是红色的
要解决此问题,我必须做些什么?TextBox控件有自己的查找和替换文本的方法。请看这篇文章(它是VB.NET,但我希望您能理解):我打赌您只是在对文本字符串调用Replace,您需要做的是这样的:
richTextBox1.Text = richTextBox1.Text.Replace(Environment.NewLine, "");
这里的关键是,您需要将函数的结果分配给富文本框的文本,否则什么都不会发生。请参阅,字符串是不可变的,每当您对其中一个字符串执行操作时,您必须将操作的结果分配给某个对象(即使原始变量也可以工作)否则什么也不会发生。我认为这有助于您:
StringBuilder strbld = new StringBuilder();
for (int i = 0; i < this.richTextBox1.Text.Length; i++)
{
char c = this.richTextBox1.Text[i];
if (c.ToString() != "\n")
strbld.Append(c);
}
MessageBox.Show(strbld.ToString());
:-|这将起作用:
// Create a temporary buffer - using a RichTextBox instead
// of a string will keep the RTF formatted correctly
using (RichTextBox buffer = new RichTextBox())
{
// Split the text into lines
string[] lines = this.richTextBox1.Lines;
int start = 0;
// Iterate the lines
foreach (string line in lines)
{
// Ignore empty lines
if (line != String.Empty)
{
// Find the start position of the current line
start = this.richTextBox1.Find(line, start, RichTextBoxFinds.None);
// Select the line (not including new line, paragraph etc)
this.richTextBox1.Select(start, line.Length);
// Append the selected RTF to the buffer
buffer.SelectedRtf = this.richTextBox1.SelectedRtf;
// Move the cursor to the end of the buffers text for the next append
buffer.Select(buffer.TextLength, 0);
}
}
// Set the rtf of the original control
this.richTextBox1.Rtf = buffer.Rtf;
}
很抱歉,您的问题不是很清楚。当您进行替换时会发生什么情况?您是否可以包含所需内容的“before”和“after”示例?这将连接文本,并删除格式设置
string strRtf = richTextBox1.Rtf.Replace("\\par\r\n", " ");
strRtf = strRtf.Replace("\\line", " ");
richTextBox2.Rtf = strRtf;
// Create a temporary buffer - using a RichTextBox instead
// of a string will keep the RTF formatted correctly
using (RichTextBox buffer = new RichTextBox())
{
// Split the text into lines
string[] lines = this.richTextBox1.Lines;
int start = 0;
// Iterate the lines
foreach (string line in lines)
{
// Ignore empty lines
if (line != String.Empty)
{
// Find the start position of the current line
start = this.richTextBox1.Find(line, start, RichTextBoxFinds.None);
// Select the line (not including new line, paragraph etc)
this.richTextBox1.Select(start, line.Length);
// Append the selected RTF to the buffer
buffer.SelectedRtf = this.richTextBox1.SelectedRtf;
// Move the cursor to the end of the buffers text for the next append
buffer.Select(buffer.TextLength, 0);
}
}
// Set the rtf of the original control
this.richTextBox1.Rtf = buffer.Rtf;
}