472,969 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Formatting text in a RichTextBox in WindowsForm


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
3 7107
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neil | last post by:
Is there an editable RTF textbox control which allows the user to apply bold, italic, etc.? I tried the Microsoft Rich Textbox Control, but there doesn't seem to be a way to allow the user to...
0
by: Hariharan S | last post by:
Hi Guys, I have a string, say "Hello World" and would like to format the way I want to (say, wanna have the Hello to be bold italic and the World to be of font size 14 or so) and put it in the...
2
by: Hariharan S | last post by:
Hi Guys, I have a string, say "Hello World" and would like to format the way I want to (say, wanna have the Hello to be bold italic and the World to be of font size 14 or so) and put it in the...
0
by: Radu.Micu | last post by:
I have a RichTextBox on my form and I want to save the text that a user writes into it in a AccessDB but I want to save the formatted text not only plain txt I have seen in some programs that the...
1
by: Kevin | last post by:
Hello, Does anyone know how to implement find and replace in a RichTextBox in a way that does not lose the text formatting? As a first attempt I tried rtb.Rtf.Replace(sFindText,...
2
by: Chris Rollock | last post by:
on msdn: "you can initialize the Rtf property to a string that contains the text to display, including the RTF codes that determine how the text should be formatted." Nowhere can I find an example...
0
by: ronchese | last post by:
Hello All. I'm using the RichTextBox control to show text to user that can do the primary font formatting: fontname, size, color, bold, italic and underline. Unlike the RichTextBox used in VB...
2
by: Mystique | last post by:
Hello Is there any way to format text with tags (something like html tags) in a RichTextBox? Something similar to this: string aStr; aStr = "<b>this is bold</bthis is normal"; ...
2
by: drfreaky | last post by:
Hello, I want to use a richtextbox for logging status messages. I have three kind of message types and want to use different formatting for them. My problem is, I change the color, add the text...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.