473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

[c#] join lines in richtextbox

on a windows form, i have RichTextBox, with some text, in several lines. and one button on a form.

i wolud like when i click on that button, to join all richtextbox of lines in one line, but not to loose text style (like font family, color, etc.)

i can not do it with Replace, like \r\n, and not with the Replace(Environment.NewLine, "")........ :-((

i have also tryed to replace \par and \pard, but still no luck.......

please help!!!
Oct 28 '09 #1
3 4072
GaryTexmo
1,501 Expert 1GB
I bet I know what you did :D

You likely went...

Expand|Select|Wrap|Line Numbers
  1. myRichTextBox.Rtf.Replace("\\par", "");
You actually want to do...

Expand|Select|Wrap|Line Numbers
  1. myRichTextBox.Rtf = myRichTextBox.Rtf.Replace("\\par", "");
The string replace method doesn't modify the actual string, it returns the modified string instead, so you need to assign it. I gave this a try and it worked for me, though there was some unexpected side effects. I'm not sure if you should replace the par... I don't really know the rich text format offhand... let me play around a bit more.

** Update:
Ok, it looks like the "\par" code is the newline, but there's also a "\pard" code in there. Since the string.Replace method isn't very choosy, it will knock out a "\pard" code and make it a "\d" code, which was generating my issue.

Knowing nothing about the Rtf format, I blindly replaced "\pard" with blank before I did "\par" and it worked, but I'd highly recommend making a better replace function on your own to make sure you knock out exactly what you want.

Maybe someone else has a suggestion for a "safer" way of doing this...?
Oct 28 '09 #2
nope
:-(

i had sevaral attempts to solve this... all of them was with code like

myRichTextBox.Rtf = myRichTextBox.Rtf.Replace("\\par", "");

i tryed with
\\pard
\\pad
\\n
\\r\\n
Enviroment.Newline

etc etc, but i can not get result i want...

i simply can not understand where is the problem... is maybe something to do with character encoding?
Oct 28 '09 #3
GaryTexmo
1,501 Expert 1GB
Well, my final solution replaced \pard with nothing, then \par with nothing. I'm guessing the control was smart enough to figure out when something was missing because when I checked the text after the replacement, the \pard was back but the \par wasn't.

Oh! I forgot to mention, since the richtextbox sorts itself out when there are errors, you have to do the change as such...

Expand|Select|Wrap|Line Numbers
  1. string modifyRtf = myRichTextBox.Rtf;
  2. modifyRtf = modifyRtf.Replace("\\pard", "");
  3. modifyRtf = modifyRtf.Replace("\\par", "");
  4. myRichTextBox.Rtf = modifyRtf;
The way I went about this was to do a Console.WriteLine on myRichTextBox.Rtf and see what was there. Then I did my replacements and outputted the Rtf again to compare. That's how I saw the issue with \pard.

Maybe doing that kind of comparison would help you work through it?

**** IMPORTANT ****
Here's the specification for the RTF format...
http://www.microsoft.com/downloads/d...displaylang=en

On page 144, line 32 it mentions that \par is indeed what a carriage return/line feed is. However, \pard is something entirely different, so you'll definitely want to make your own replacement method so you don't blow away \pard when replacing \par with nothing.
Oct 28 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: JDeats | last post by:
Is it possible to use GDI+ to draw: lines, points, shapes, etc... inside a RichTextBox? It seems VS.NET does this with the code edit window (left margin code collapse/expand boxes)? Best, Jeremy
1
by: Thomas Kehl | last post by:
Hi! I have a RichTextBox. I set the Property RightMargin of this Box to a value, that i will not show the horizontal scrollbar. Now, the RichTextBox Wrap the Lines automatically. Now I would...
2
by: Thorsten moeller | last post by:
Hi, i have a Textbox and amethod adding lines to the Textbox. There are two kind lines, depending on a check to be valid or invalid. Is it possible to color the lines or the text in a textbox...
0
by: Du | last post by:
I tried to search google and the news group, nothing turned up. I used the following code, but some how it doesn't work rchtextbox.Lines = "hello"; rchtextbox is my RichTextBox thanks
1
by: Benny Raymond | last post by:
I have lines of data coming from a server being written to a RichTextBox... The problem is that if I let this program run for a long period of time there are hundreds of lines of text showing up in...
4
by: Juan Romero | last post by:
Hey guys, Does anyone know how to find out the visible lines in the richtextbox control? For example, let's say the control has 30 lines, but due to size it can only display 10. The current...
0
by: OmniTech via .NET 247 | last post by:
I am having trouble updating lines in a richtextbox. The rtb.lines.setvalue("text", linenumber) runs, but does not update the control in the form. The code follows... This apps automatically...
2
by: None | last post by:
How do i write lines with different colors in a textbox? I've tried using the forecolor property and it just takes the last color. eg. TextBox1.ForeColor = Color.Red TextBox1.Text = "test" & "...
3
by: kaushalgajjar | last post by:
Hi, I am into a trouble since 1 week and unable to find the solution from any community or site. I have created a small program that i want to print along with horizontal lines (like a notebook...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.