Formatting text in a RichTextBox in WindowsForm

M

michael sorens

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?
 
G

Guest

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
 
L

Linda Liu [MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

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

michael sorens

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top