By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Formatting text in a RichTextBox in WindowsForm

P: n/a

The documentation for the RichTextBox is sketchy at best. I want to do a
very simple task but I cannot find information on this. I am using a
RichTextBox as an output window. Some text I want to just add plain (e.g..
rtf.Text += "new stuff..."; ) but some text, when added, I want to be in a
different color or font size. Find() will not work because the text will
vary. I have delimiters on it so I could recognize it with a regular
expression (e.g. "\[.*?\]") but Find only takes constant strings. The
issue, then, is being able to select by regular expression, after which I
could set the SelectionFont or SelectionColor. Also, I observe that whenI
add new text, previous formatting I have done disappears. So I have to
reapply all formatting each time I add something; is that the way it is
supposed to work?
Nov 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
If you are adding to the end of the text, I found it easier to have a second
non visibel RTB and set the text of that. Then select the text and apply all
the formatting. Then you take the RTF text from it and append it to the RTF
text of the main RTB.
If you set the Text property then it will lose all formatting as you are
giving it a new piece of plain text to show. It isnt able to determin if the
the new text is the old text plus some as they are difference string
instances.

HTH

Ciaran O'Donnell

"michael sorens" wrote:
>
The documentation for the RichTextBox is sketchy at best. I want to do a
very simple task but I cannot find information on this. I am using a
RichTextBox as an output window. Some text I want to just add plain (e.g..
rtf.Text += "new stuff..."; ) but some text, when added, I want to be in a
different color or font size. Find() will not work because the text will
vary. I have delimiters on it so I could recognize it with a regular
expression (e.g. "\[.*?\]") but Find only takes constant strings. The
issue, then, is being able to select by regular expression, after which I
could set the SelectionFont or SelectionColor. Also, I observe that when I
add new text, previous formatting I have done disappears. So I have to
reapply all formatting each time I add something; is that the way it is
supposed to work?
Nov 10 '06 #2

P: n/a
Hi Michael,

The introduction of RichTextBox class in MSDN may be a little sketchy. For
more information on how to use RichTextBox control in Windows Forms, you
could visit the following link.

http://msdn2.microsoft.com/en-us/library/aa467125.aspx

As for your first question, I am sorry that RichTextBox doesn't support
such a function at present. The Find method of the RichtTextBox can only
take a concrete string as its parameter. If you do need this function, I
recommend you to write a method to encapsulate the code for this function
(you could search the Internet for the arithmetic) and then call this
method whenever you want.

You have mentioned that you want to format some text when added. When a
text is added to a RichTextBox control, the SelectionStart property of the
RichTextBox is set to the end position of the added text. We could make use
of this feature to select the added text and then set the SelectionFont or
SelectionColor property of the RichTextBox.

The following is a sample.

this.richTextBoxPrintCtrl1.AppendText("abcde");
// set the SelectionStart property to the start position of the added text
this.richTextBoxPrintCtrl1.SelectionStart -= 5;
// set the SelectionLength property to the length of the added text
this.richTextBoxPrintCtrl1.SelectionLength = 5;
this.richTextBoxPrintCtrl1.SelectionFont = new Font("Times New Roman", 20);
this.richTextBoxPrintCtrl1.SelectionColor = Color.Blue;

As for your second question, could you tell me how you add a text in the
RichTextBox control? If we use the following code to add a text, the
previous format will be lost, because the Text property of the RichTextBox
control is re-assigned as as whole.

this.richTextBoxPrintCtrl1.Text += "abcde";

Instead, we could use the AppendText method of the RichTextBox control to
add a text.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 10 '06 #3

P: n/a
Thank you for both the documentation pointer and the code fragment. That
was just what I needed. (And yes, I had tried a simple rtb.Text += ...
because I had missed the AppendText method.)


Nov 10 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.